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开发者_如何学Go'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 Eid
property is the same, you should use GroupBy(MatchedFeature => MatchedFeature.Eid)
.
If you leave out the .Eid
it will only group those features together which are entirely the same - not the ones that have the same Eid
.
You have to do MatchedFeature => MatchedFeature.Eid
instead of MatchedFeature => MatchedFeature
which is an instance of your Custom class hense you get one group per instance of your custom class I believe.
精彩评论