Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. var firstList = new List<Data1>();
  2. var secondList = new List<Data2>();
  3. var sorted = new List<Data2>();
  4. firstList.Each(zz => { sorted.Add(secondList.First(z => z.id_from_firstlist == zz.Id));});
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10.  
  11. List<Item1> myList = Test.getmyList();
  12. List<Item2> externList = Test.getExternList();
  13.  
  14. Test.print(myList, "Мой список:");
  15. Test.print(externList, "Внешний список до сортировки:");
  16.  
  17. externList = externList.OrderBy(x => myList.FindIndex(y => x.Name == y.Name)).ToList();
  18.  
  19. Test.print(externList, "Внешний список после сортировки:");
  20.  
  21. }
  22.  
  23.  
  24. private static List<Item1> getmyList()
  25. {
  26. Item1[] items = new Item1[] {
  27. new Item1 {id = 1, Name = "P1"},
  28. new Item1 {id = 2, Name = "P2"},
  29. new Item1 {id = 3, Name = "P3"},
  30. new Item1 {id = 4, Name = "P4"},
  31. new Item1 {id = 5, Name = "P5"}
  32. };
  33. return items.ToList();
  34. }
  35.  
  36. private static List<Item2> getExternList()
  37. {
  38. Item2[] items = new Item2[] {
  39. new Item2 { Name = "P5"},
  40. new Item2 { Name = "P4"},
  41. new Item2 { Name = "P3"},
  42. new Item2 { Name = "P2"},
  43. new Item2 { Name = "P1"}
  44. };
  45. return items.ToList();
  46. }
  47.  
  48. private static void print<T>(IEnumerable<T> list, string message = "")
  49. {
  50. Console.WriteLine(message);
  51. foreach (var item in list)
  52. {
  53. Console.WriteLine(item.ToString());
  54. }
  55. }
  56.  
  57. }
  58.  
  59. class Item1
  60. {
  61. public int id { get; set; }
  62. public string Name { get; set; }
  63.  
  64. public override string ToString()
  65. {
  66. return Name;
  67. }
  68. }
  69.  
  70. class Item2
  71. {
  72. public string Name { get; set; }
  73. public override string ToString()
  74. {
  75. return Name;
  76. }
  77. }
  78.  
  79. class Test
  80. {
  81. static void Main(string[] args)
  82. {
  83. var l1 = new[]
  84. {
  85. new C1() { Id = 3, Name = "Иоганн" },
  86. new C1() { Id = 1, Name = "Ваня" },
  87. new C1() { Id = 2, Name = "Джонни" }
  88. };
  89. var l2 = new[]
  90. {
  91. new C2() { Id = 1, Age = 3 },
  92. new C2() { Id = 3, Age = 331 },
  93. new C2() { Id = 2, Age = 25 }
  94. };
  95.  
  96. var result =
  97. from main in l1
  98. join aux in l2.Select((c2, idx) => new { c2.Id, idx }) on main.Id equals aux.Id
  99. orderby aux.idx
  100. select main;
  101.  
  102. foreach (var c1 in result)
  103. Console.WriteLine(c1.Name);
  104. }
  105. }
  106.  
  107. class C1
  108. {
  109. public int Id { get; set; }
  110. public string Name { get; set; }
  111. }
  112.  
  113. class C2
  114. {
  115. public int Id { get; set; }
  116. public int Age { get; set; }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement