简体   繁体   English

按条目数linq排序

[英]Order by number of entries linq

I'm trying to filter and sort data from a table based on the number of entries in one column 我正在尝试根据一列中的条目数对表中的数据进行过滤和排序

|  OrderID  | DateOrdered  | EnteredById |  OtherData  |  OtherData2  |
-----------------------------------------------------------------------
|   1       |  2/2/2017    |     3       |  asdf       |      sadfsf  |
|   2       |  2/2/2017    |     4       |  asdf       |      sadfsf  |
|   3       |  2/3/2017    |     4       |  asdf       |      sadfsf  |
|   4       |  2/4/2017    |     3       |  asdf       |      sadfsf  |
|   5       |  2/4/2017    |     4       |  asdf       |      sadfsf  |
|   6       |  2/6/2017    |     5       |  asdf       |      sadfsf  |

This is a C# project, there is a working model set up for the table (Orders) I need to use Linq with Entity to return an array of objects, the whole row, sorted by the number of orders entered by each EnteredById and only where there has been more than one entry by that EnteredById. 这是一个C#项目,为表设置了一个工作模型(订单),我需要使用带有实体的Linq返回对象数组,整行,并按每个EnteredById输入的订单数进行排序,并且仅在该EnteredById包含多个条目。

In the end, it should be: 最后,应该是:

| OrderID | DateOrdered | EnteredById | OtherData | OtherData2 | ----------------------------------------------------------------------- | 2 | 2/2/2017 | 4 | asdf | sadfsf | | 3 | 2/3/2017 | 4 | asdf | sadfsf | | 5 | 2/4/2017 | 4 | asdf | sadfsf | | 4 | 2/4/2017 | 3 | asdf | sadfsf | | 1 | 2/2/2017 | 3 | asdf | sadfsf |

The code I've written is an absolute mess so I haven't posted it here. 我写的代码绝对是一团糟,所以我没有在这里发布。 Does anybody have the secret Linq answer? 有人知道Linq的答案吗?

var result = objListOrder.GroupBy(o=>o.EnteredById).Where((x,groupid)=> x.Count()>1).SelectMany((x,groupid)=>x).OrderBy(o=>o.EnteredById); 

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

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