fork(1) download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5.  
  6. public class Test
  7. {
  8.  
  9.  
  10. public class Stock {
  11. public string Name {get; set;}
  12. public List<int> Values {get; set;}
  13. public float Price {get; set;}
  14.  
  15. public override bool Equals(object obj) {
  16. if (obj == this) return true;
  17. var other = obj as Stock;
  18. if (other == null) return false;
  19. return Name.Equals(other.Name)
  20. && Price == other.Price
  21. && Values.SequenceEqual(other.Values);
  22. }
  23. public override int GetHashCode() {
  24. return Name.GetHashCode()
  25. + Price.GetHashCode()
  26. + Values.Sum();
  27. }
  28.  
  29.  
  30. }
  31.  
  32. // Stock collection
  33.  
  34.  
  35.  
  36. public static void Main()
  37. {
  38.  
  39. var stocks = new List<Stock> {
  40. new Stock(){Name="Prod1", Values=new List<int>{1, 2, 3, 0}, Price=5.0f},
  41. new Stock(){Name="Prod1", Values=new List<int>{1, 2, 3, 0}, Price=5.0f},
  42. new Stock(){Name="Prod11", Values=new List<int>{1, 0, 3, 1}, Price=8.0f},
  43. new Stock(){Name="Prod11", Values=new List<int>{1, 0, 3, 1}, Price=8.0f},
  44. new Stock(){Name="Prod18", Values=new List<int>{0, 0, 4, 1}, Price=4.5f},
  45. new Stock(){Name="Prod20", Values=new List<int>{4, 0, 0, 2}, Price=9.9f},
  46. new Stock(){Name="Prod20", Values=new List<int>{4, 0, 0, 2}, Price=9.9f},
  47. new Stock(){Name="Prod29", Values=new List<int>{2, 1, 0, 1}, Price=7.2f}
  48. };
  49. var res = stocks.GroupBy(x => x, (x, g) => new { Count = g.Count(), Values = x}).ToList();
  50. foreach (var item in res) {
  51. Console.WriteLine("{0} {1} at {2} ({3})", item.Count, item.Values.Name, item.Values.Price, string.Join(", ", item.Values.Values.ToArray()));
  52. }
  53. }
  54. }
Success #stdin #stdout 0.06s 24712KB
stdin
Standard input is empty
stdout
2 Prod1 at 5 (1, 2, 3, 0)
2 Prod11 at 8 (1, 0, 3, 1)
1 Prod18 at 4.5 (0, 0, 4, 1)
2 Prod20 at 9.9 (4, 0, 0, 2)
1 Prod29 at 7.2 (2, 1, 0, 1)