fork(2) download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public class Test
  6. {
  7. static Random rand = new Random();
  8.  
  9. static void Main(string[] args)
  10. {
  11. var products = new List<Product>();
  12. for (int i = 0; i < 10; i++)
  13. {
  14. products.Add(new Product
  15. {
  16. Name = "Product " + (i + 1),
  17. StartDetailRating = GenerateRandomVotes()
  18. });
  19. }
  20. var productWithVotes = products.Select(x => new
  21. {
  22. Product = x,
  23. VoteAverage = x.StartDetailRating.Any() ? x.StartDetailRating.Average(r => r.VoteValue) : 0
  24. });
  25. foreach (var result in productWithVotes)
  26. {
  27. Console.WriteLine("{0} scored an average of {1} votes", result.Product.Name, result.VoteAverage);
  28. }
  29. }
  30.  
  31. static List<StarDetailRating> GenerateRandomVotes()
  32. {
  33. var count = rand.Next(50);
  34. var results = new List<StarDetailRating>(count);
  35. for (int i = 0; i < count; i++)
  36. {
  37. results.Add(new StarDetailRating
  38. {
  39. VoteValue = rand.Next(0, 15)
  40. });
  41. }
  42. return results;
  43. }
  44. }
  45.  
  46. public class Product
  47. {
  48. public string Name { get; set; }
  49. public IEnumerable<StarDetailRating> StartDetailRating { get; set; }
  50. }
  51.  
  52. public class StarDetailRating
  53. {
  54. public int VoteValue { get; set; }
  55. }
Success #stdin #stdout 0.04s 33808KB
stdin
Standard input is empty
stdout
Product 1 scored an average of 6.85714285714286 votes
Product 2 scored an average of 8 votes
Product 3 scored an average of 7.3 votes
Product 4 scored an average of 6.82926829268293 votes
Product 5 scored an average of 7.09090909090909 votes
Product 6 scored an average of 7.58823529411765 votes
Product 7 scored an average of 6.62857142857143 votes
Product 8 scored an average of 6.125 votes
Product 9 scored an average of 6.83333333333333 votes
Product 10 scored an average of 6.51282051282051 votes