I have a collection List<CustomClass> which I am trying to find a way to return a field which corresponds to another field that occurs the highest number of times.
I’ve asked a similar question in the past, but I’m not sure how to extend the logic to a custom class.
For instance.
My CustomClass has three public properties:
Title
Mid
Eid
And a List<CustomClass> MatchedFeatures = List<CustomClass>(); is populated in a loop.
I need to perform a Linq operation to return the Mid of the class that has the largest number of occurring identical Eid‘s. So we are counting the identical Eid columns.
I have tried using
var TopResult = MatchedFeatures.GroupBy(MatchedFeature => MatchedFeature).OrderByDescending(group => group.Count()).FirstOrDefault();
But it doesn’t return what I am looking for.
Can anyone see where I am going wrong? I am using C#
Here is the previous asked question, which seems to work fine. Now how to extend it in this case…?
Simple LINQ question in C#
Many thanks,
Brett
If you want to group those features where the
Eidproperty is the same, you should useGroupBy(MatchedFeature => MatchedFeature.Eid).If you leave out the
.Eidit will only group those features together which are entirely the same – not the ones that have the sameEid.