fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Foo
  6. {
  7. public int ID { get; set; }
  8. // ID will remain unique in collection
  9.  
  10. public string Name { get; set; }
  11. }
  12.  
  13. public class Test
  14. {
  15. public static void Main()
  16. {
  17. List<Foo> sc = new List<Foo>();
  18.  
  19. sc.Add(new Foo()
  20. {
  21. ID = 0,
  22. Name = "Michael"
  23. });
  24.  
  25. sc.Add(new Foo()
  26. {
  27. ID = 2,
  28. Name = "Natasha"
  29. });
  30.  
  31. sc.Add(new Foo()
  32. {
  33. ID = 1,
  34. Name = "Casandra"
  35. });
  36.  
  37. List<string> dt = new List<string>(); //For testing replaced just by string.
  38.  
  39. dt.Add("Michael");
  40.  
  41. dt.Add("Natasha");
  42.  
  43. dt.Add("Casandra");
  44.  
  45. var ordered = dt.Select(i => sc.First(c => c.Name == i)).OrderBy(i => i.ID);
  46.  
  47. foreach (var item in ordered) { Console.WriteLine(item.Name); }
  48. }
  49. }
Success #stdin #stdout 0.04s 34064KB
stdin
Standard input is empty
stdout
Michael
Casandra
Natasha