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<string> sourceNames = new List<string>() { "A", "B", "C", "A", "B", "C" };
  10. List<string> targetNames = new List<string>() { "Z", "Y", "X", "W", "V", "U" };
  11.  
  12. var lookup = sourceNames
  13. .Select((Source, i) => new { Target = targetNames.ElementAt(i), Source})
  14. .GroupBy(x => x.Source)
  15. .ToDictionary(g => g.Key, g => g.Select(x => x.Target));
  16.  
  17. foreach(var kv in lookup)
  18. Console.WriteLine("{0} has destinations {1}"
  19. , kv.Key
  20. , string.Join(",", lookup[kv.Key].ToArray()));
  21. }
  22. }
Success #stdin #stdout 0.06s 37216KB
stdin
Standard input is empty
stdout
A has destinations Z,W
B has destinations Y,V
C has destinations X,U