C# Linq 问题 GROUP BY?

发布于 2024-10-19 13:49:40 字数 832 浏览 5 评论 0原文

我有一个集合 List ,我试图找到一种方法来返回与出现次数最多的另一个字段相对应的字段。 我过去问过类似的问题,但我不确定如何将逻辑扩展到自定义类。 例如。

我的 CustomClass 具有三个公共属性:

Title
Mid
Eid

和一个 ListMatchedFeatures = List(); 在循环中填充。

我需要执行 Linq 操作来返回出现最多相同 Eid 的类的 Mid。因此,我们正在计算相同的 Eid 列。

我尝试过使用

var TopResult = MatchedFeatures.GroupBy(MatchedFeature => MatchedFeature).OrderByDescending(group => group.Count()).FirstOrDefault();

但它没有返回我正在寻找的内容。

谁能看到我哪里出错了?我正在使用 C#

这是之前提出的问题,它似乎工作得很好。现在在这种情况下如何扩展它......? C# 中的简单 LINQ 问题

非常感谢, 布雷特

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城已无爱 2024-10-26 13:49:40

如果您想要对 Eid 属性相同的要素进行分组,则应使用 GroupBy(MatchedFeature => MatchedFeature.Eid)

如果您省略 .Eid,它只会将那些完全相同的功能分组在一起 - 而不是具有相同 Eid 的功能。

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.

没企图 2024-10-26 13:49:40

你必须做 MatchedFeature =>; MatchedFeature.Eid 而不是 MatchedFeature => MatchedFeature 这是您的自定义类的一个实例,因此我相信您的自定义类的每个实例都会获得一个组。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文