Reputation: 185
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.
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?
Upvotes: 0
Views: 245
Reputation: 222657
var result = objListOrder.GroupBy(o=>o.EnteredById).Where((x,groupid)=> x.Count()>1).SelectMany((x,groupid)=>x).OrderBy(o=>o.EnteredById);
Upvotes: 2