C# Linq that includes sorting and grouping

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


C# Linq that includes sorting and grouping



This is my code:


public class Photos
{
public long PhotoLabel { get; set; }
public int UserID { get; set; }
}

List<Photos> photolist = new List<Photos>();
var result1 = photolist.OrderByDescending(p => p.PhotoLabel).ThenBy(r => r.UserID).ToList();



If I display the contents now, this is what I get (First sorted in descending order of PhotoLabel and then sorted by UserID:


|------|---------------------|---------------------|
| Row | UserID | PhotoLabel |
|----------------------------|---------------------|
| 1 | 92 | 20180729181046 |
|----------------------------|---------------------|
| 2 | 92 | 20180729181041 |
|----------------------------|---------------------|
| 3 | 92 | 20180729181037 |
|----------------------------|---------------------|
| 4 | 88 | 20180729174415 |
|----------------------------|---------------------|
| 5 | 88 | 20180729174405 |
|----------------------------|---------------------|
| 6 | 04 | 20180729174358 |
|----------------------------|---------------------|
| 7 | 1 | 20170924183847 |
|----------------------------|---------------------|
| 8 | 1 | 20170921231422 |
|----------------------------|---------------------|
| 9 | 1 | 20170920194624 |
|----------------------------|---------------------|
| 10 | 32 | 20170820114728 |
|----------------------------|---------------------|
| 11 | 32 | 20170820114725 |
|----------------------------|---------------------|
| 12 | 32 | 20170820114421 |
|----------------------------|---------------------|
| 13 | 32 | 20170820114416 |
|----------------------------|---------------------|
| 14 | 1 | 20170225151023 |
|----------------------------|---------------------|
| 15 | 1 | 20170225151000 |
|----------------------------|---------------------|
| 16 | 1 | 20170225150957 |
|----------------------------|---------------------|



From the sorted table above, this is what I want to achieve:



Display groups of UserIDs and PhotoLabels where UserIDs appear 3 or more times in one group (eg: rows 4 and 5 where UserID=88 and row 6 where UserID=04 should be eliminated since the UserID=88 appears just twice in the group and UserID=04 appears only once in the group).



Display only the top most group of UserIDs and exclude any repeating UserIDs (eg: rows 7,8 and 9 displays the UserID=1 group. Don't display any other UserID=1 group such as rows 14,15 and 16.



The expected result from query should be:


|------|---------------------|---------------------|
| Row | UserID | PhotoLabel |
|----------------------------|---------------------|
| 1 | 92 | 20180729181046 |
|----------------------------|---------------------|
| 2 | 92 | 20180729181041 |
|----------------------------|---------------------|
| 3 | 92 | 20180729181037 |
|----------------------------|---------------------|
| 7 | 1 | 20170924183847 |
|----------------------------|---------------------|
| 8 | 1 | 20170921231422 |
|----------------------------|---------------------|
| 9 | 1 | 20170920194624 |
|----------------------------|---------------------|
| 10 | 32 | 20170820114728 |
|----------------------------|---------------------|
| 11 | 32 | 20170820114725 |
|----------------------------|---------------------|
| 12 | 32 | 20170820114421 |
|----------------------------|---------------------|
| 13 | 32 | 20170820114416 |
|----------------------------|---------------------|



Thank you so much in in advance! :-)





What have you tried so far, and where are you having trouble?
– John Wu
28 mins ago





does it require to achieve in 1 LINQ? or it is a function output in .NET
– SKLTFZ
20 mins ago





@SKLTFZ it doesn't have to be 1 Linq. Anything that does the job efficiently would do. The table has tons of rows so efficiency is important.
– sonnyk2016
17 mins ago





i think the requirement looks like unable to achieve in LINQ, but it can be implemented easily by a function with a pivot and a list container for the return list
– SKLTFZ
14 mins ago




2 Answers
2



Please try this!


List<Photos> photolist = photolist.GroupBy(p => p.UserID)
.Where(g => g.Count() > 2)
.Select(p => new
{
UserId = p.UserID,
PhotoLabel = p.PhotoLabel

})
.ToList();



With the two syntaxes:


var minCount = 3;
var topN = 10;

var res = result1.GroupBy(p => p.UserID)
.Where(g => g.Count() >= minCount)
.Select(p => p)
.Take(topN);

var res2 = (from p in result1
group p by p.UserID into g
where g.Count() >= minCount
select p).Take(topN);



Notice that I used yout result1 set as it it already sorted. If I was to start from photolist, I would also need to apply the sorting, because of the TOP N requirement.


result1


photolist






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived