加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#:如何根据字符串列表对对象列表进行排序

发布时间:2020-12-15 04:00:10 所属栏目:百科 来源:网络整理
导读:我有两个列表 ListString l_lstNames = new ListString { "A1","A3","A2","A4","A0" };ListTest l_lstStudents = new ListTest { new Test { Age = 20,Name = "A0" },new Test { Age = 21,Name = "A1" },new Test { Age = 22,Name = "A2" },new Test { Age =
我有两个列表
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()

我觉得这个意思很清楚.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读