fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var foos = Enumerable.Range(1, 1000).Select(_ => new Foo()).ToList();
  10.  
  11. new Test().GroupChanges(foos);
  12. }
  13.  
  14. public void GroupChanges<TResult>(List<TResult> changes)
  15. {
  16. const string KeyPropertyName = "TransactionSequence";
  17.  
  18. var changeSet = changes
  19. .GroupBy(change => /*change.TransactionSequence */
  20. (byte[])change.GetType().GetProperty(KeyPropertyName).GetValue(change)
  21. , new ArrayComparer<byte>())
  22. .ToList();
  23.  
  24. Console.WriteLine(changeSet.Count);
  25. }
  26. }
  27.  
  28. public class Foo
  29. {
  30. private static readonly byte[][] Keys = new[] {
  31. Guid.NewGuid().ToByteArray(),
  32. Guid.NewGuid().ToByteArray(),
  33. Guid.NewGuid().ToByteArray()
  34. };
  35.  
  36. private static readonly Random rand = new Random();
  37.  
  38. public Foo()
  39. {
  40. this.TransactionSequence = Keys[rand.Next(3)];
  41. }
  42.  
  43. public byte[] TransactionSequence { get; private set; }
  44. }
  45.  
  46. public class ArrayComparer<T> : IEqualityComparer<T[]>
  47. {
  48. public bool Equals(T[] x, T[] y)
  49. {
  50. return x.SequenceEqual(y);
  51. }
  52.  
  53. public int GetHashCode(T[] obj)
  54. {
  55. return obj.GetHashCode();
  56. }
  57. }
Success #stdin #stdout 0.02s 132800KB
stdin
Standard input is empty
stdout
3