我有两个列表
List<String> l_lstNames = new List<String> { "A1","A3","A2","A4","A0" }; List<Test> l_lstStudents = new List<Test> { new Test { Age = 20,Name = "A0" },new Test { Age = 21,Name = "A1" },new Test { Age = 22,Name = "A2" },new Test { Age = 23,Name = "A3" },new Test { Age = 24,Name = "A4" },};
测试就是类
public class Test { public String Name; public Int32 Age; }
我需要根据l_lstNames对l_lst学生中的项进行排序.所以排序的列表会像,
List<Test> l_lstStudents = new List<Test> { new Test { Age = 21,new Test { Age = 20,};
现在我正在用这样做.
喜欢
>创建一个新的测试对象列表.
>迭代l_lstNames的循环,并从l_lstStudent中获取Test对象,并将其添加到新创建的列表中.最后将新列表分配给l_lstStudent
请帮我以简单的方式(Linq或Lambda)
解决方法
尝试这个:
l_lstStudents = l_lstStudents.OrderBy(s => l_lstNames.IndexOf(s.Name)).ToList()
我觉得这个意思很清楚.