0

I currently have a controller that's returning a list of results. Some results get multiple hits, for example:

result x,
result x,
result x,
result y,
result z

I'm then trying to sort these results by the number of hits they have with this code here:

(orgs.Organisations is a list of Organisations i.e. results)

orgs.Organisations = orgs.Organisations
                         .GroupBy(f => f.Name)
                         .SelectMany(c => c.OrderByDescending(p => p.Name.Count()))
                         .ToList();

This is the closest I think I have gotten but its still not returning them in the correct order. Any guidance would be greatly appreciated.

1 Answer 1

1

Your current query orders items within group, not the groups. Make OrderBy call before SelectMany:

orgs.Organisations = orgs.Organisations
                         .GroupBy(f => f.Name)
                         .OrderBy(g => g.Count())
                         .SelectMany(g => g)
                         .ToList();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.