Skip to main content
 首页 » 编程设计

c#之用于对集合列表进行排序的 Lambda 表达式

2024年02月04日27dudu

我的输入在xml文件下面

<Employees> 
  <Department Position="9"> 
   <Employee No="7" Status="True" /> 
   <Employee No="6" Status="True" /> 
   <Employee No="8" Status="True" /> 
</Department> 
<Department Position="4"> 
  <Employee No="7" Status="True" /> 
  <Employee No="8" Status="True" /> 
  <Employee No="6" Status="True" /> 
</Department> 
</Employees> 
 
Out put should  be  sorted  by department position  and  employee  "No" 
 
 
<Employees> 
 <Department Position="4"> 
  <Employee No="6" Status="True" /> 
  <Employee No="7" Status="True" /> 
  <Employee No="8" Status="True" /> 
 </Department> 
 <Department Position="9"> 
   <Employee No="6" Status="True" /> 
   <Employee No="7" Status="True" /> 
   <Employee No="8" Status="True" /> 
 </Department>   

我添加了下面的代码,但它要么返回“position”明智的,要么返回“No”明智的,但不会同时返回两者。

var sortSignalList = new Dictionary<int, List<string>>(); 
 
sortSignalList.OrderBy(x => x.Position).OrderBy(x=>x.No).ToList(); 

请您参考如下方法:

sortSignalList.OrderBy(x => x.Position).ThenBy(x=>x.No).ToList();