【问题标题】:Lambda expression for sorting list of collection用于对集合列表进行排序的 Lambda 表达式
【发布时间】:2018-12-06 13:41:30
【问题描述】:

我的输入在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>  

我添加了下面的代码,但它会返回“位置”或“否”,但不能同时返回。

var sortSignalList = new Dictionary<int, List<string>>();

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

【问题讨论】:

标签: c# .net linq lambda


【解决方案1】:
sortSignalList.OrderBy(x => x.Position).ThenBy(x=>x.No).ToList();

【讨论】:

    【解决方案2】:

    试试....

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

    【讨论】:

      【解决方案3】:

      列表也可以排序ThenByDescending

      var thenByDescResult =sortSignalList.OrderBy(s => s.Position).ThenByDescending(s => s.No);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-22
        相关资源
        最近更新 更多