Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据不同类的计数对对象列表进行降序排序_C#_.net_Linq_Sorting_Lambda - Fatal编程技术网

C# 根据不同类的计数对对象列表进行降序排序

C# 根据不同类的计数对对象列表进行降序排序,c#,.net,linq,sorting,lambda,C#,.net,Linq,Sorting,Lambda,我被困在这个问题上,需要根据其他列表进行降序排序。Llstu名称需要按年龄递减进行更新 public class Test { public String Name; public Int32 Age; } List<String> l_lstNames = new List<String> { "A1", "A3", "A2", "A4", "A0" }; List<Test> l_lstStudents = new List<Te

我被困在这个问题上,需要根据其他列表进行降序排序。Llstu名称需要按年龄递减进行更新

public class Test
{
    public String Name;
    public Int32 Age;
}

List<String> l_lstNames = new List<String> { "A1", "A3", "A2", "A4", "A0" };

List<Test> l_lstStudents = new List<Test> 
{ 
    new Test { Age = 33, Name = "A0" }, 
    new Test { Age = 10, Name = "A1" }, 
    new Test { Age = 50, Name = "A2" }, 
    new Test { Age = 8,  Name = "A3" }, 
    new Test { Age = 25, Name = "A4" }, 
};

// Output
List<String> l_lstNames = new List<String> { "A2", "A0", "A4", "A1", "A3" };
找到了一些sames样本,但与我要找的不匹配。感谢您的帮助。

创建具有名称到年龄映射的词典,并在order方法中使用它:

var dict = students.ToDictionary(x => x.Name, x => x.Age);

var ordered = source.OrderByDescending(x => dict[x.Name]).ToList();
或者,您可以只订购students collection,然后选择Name only:


如果只希望名称按降序排列:

var sorted = l_lstStudents           // From the list of students
    .OrderByDescending(l => l.Age)   // with the oldest student first
    .Select(s => s.Name)             // give me just the names
    .ToList();                       // in a list!

我想这就是你要找的

List<String> l_lstNames1 = (from student in l_lstStudents
                          where l_lstNames.Any(a => student.Name == a)
                          orderby student.Age descending
                          select student.Name ).ToList();


+1以获取简明示例和所需输出。完美的可能副本!事实上,我的学生名单是在dict和它只是你的一行代码得到解决方案!谢谢,谢谢。这也帮助了我。
List<String> l_lstNames1 = (from student in l_lstStudents
                          where l_lstNames.Any(a => student.Name == a)
                          orderby student.Age descending
                          select student.Name ).ToList();
List<String> l_lstNames2 = l_lstStudents.OrderByDescending(a => a.Age)
                                .Where(a => l_lstNames.Any(b => b == a.Name))
                                .Select(a => a.Name).ToList();