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. List<Destination> destinations = new List<Destination>() {
  10. new Destination() { A = 1, B = 1 },
  11. new Destination() { A = 1, B = 2 },
  12. new Destination() { A = 2, B = 1 }
  13. };
  14.  
  15. List<Source> sources = new List<Source>() {
  16. new Source() { A = 1, B = 1, C = "a" },
  17. new Source() { A = 2, B = 1, C = "b" },
  18. new Source() { A = 2, B = 1, C = "c" },
  19. new Source() { A = 1, B = 1, C = "d" },
  20. new Source() { A = 2, B = 2, C = "e" }
  21. };
  22.  
  23. var lookup = sources.ToLookup(s => new { s.A, s.B }, s => s.C);
  24. foreach (var d in destinations)
  25. d.Cs = lookup[new { d.A, d.B }].ToList();
  26.  
  27. // results
  28. Console.WriteLine("A B Cs");
  29. foreach(var d in destinations)
  30. {
  31. Console.WriteLine("{0}-{1}: {2}", d.A, d.B, String.Join(",", d.Cs.Cast<string>().ToArray()));
  32. }
  33. }
  34. }
  35.  
  36. class Source
  37. {
  38. public object A { get; set; }
  39. public object B { get; set; }
  40. public object C { get; set; }
  41. }
  42.  
  43. class Destination
  44. {
  45. public object A { get; set; }
  46. public object B { get; set; } // (A,B) is a unique key
  47. public List<object> Cs { get; set; }
  48. public object D { get; set; }
  49. }
Success #stdin #stdout 0.05s 33992KB
stdin
Standard input is empty
stdout
A B  Cs
1-1: a,d
1-2: 
2-1: b,c