简体   繁体   English

用linq命令对父子进行排序

[英]Sorting Parent-Child with orderby linq

Input: 输入:

在此处输入图片说明

I want to develop linq query order by job name(parents) and sequence of child with in list of object 我想通过对象名称中的工作名称(父母)和孩子的顺序来开发linq查询顺序

i tried below query (not working) 我试过下面的查询(不工作)

qry = qry.OrderBy(j => j.Id).ThenBy(j => j.jobName).ThenBy(j => j.documentGroupId);

Expected Output: 预期产量:

在此处输入图片说明

Any one help!!! 任何人的帮助!!!

As far as I can see, the expected result is ordering by ID column first, but from 1004 to 1001 which means you need to order it by descending. 据我所知,预期的结果是先按ID列排序,但从1004到1001,这意味着您需要按降序对其进行排序。 Use Enumerbale.OrderByDescending and Enumerable.ThenByDescending instead. 请改用Enumerbale.OrderByDescendingEnumerable.ThenByDescending

You should try that: 您应该尝试:

var result = qry.OrderByDescending(j => j.Id).
          ThenBy(j => j.SubId).
          ThenBy(j => j.JobName).
          ThenByDescending(j => j.DocumentGroupId).ToList();

Also, it would be great to have more details on how things should be sorted. 同样,拥有更多有关如何分类的细节也将是很棒的。 Not just expected result screen. 不只是预期结果屏幕。

Like this? 像这样?

qry =
  qry
    .GroupBy(j => j.Id)
    .OrderBy(group => group
      .Single(j => j.SubId == 0)
      .Name)
    .SelectMany(group => group
      .OrderBy(j => j.SubId))
    .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM