使用 C# 泛型根据列的值检索最常见的元素

发布于 2024-11-06 00:26:04 字数 502 浏览 4 评论 0原文

我有下面的代码从 PetsAgeDataTable 检索最常见的成熟度。我可以使用此代码,但我需要在其他列上执行相同的操作。因此,我需要使其通用并传递 lambda 表达式,以便可以重用它。我只是花了太多时间试图解决这个问题,询问是否有人愿意让我知道如何......干杯/Jan Jensen

var Petmaturity = from p in PetsAgeDataTable
                         where p.Maturity != null // 
                         group p by new { p.Maturity, p.PetId } into gp
                   select new { Maturity = gp.Key.Maturity, Count = gp.Coeunt() };
 var element= Petmaturity.OrderByDescending(s => s.Count).First()

I have the code below that retrieves the most common maturity from PetsAgeDataTable. I have this code working but I will need to do the same trick on an other column. I therefore need to make it generic and pass a lambda expression so it can be reused. I'm just spending far too much time trying to figure it out there asking if anyone would be kind enough to let me know how... Cheers / Jan Jensen

var Petmaturity = from p in PetsAgeDataTable
                         where p.Maturity != null // 
                         group p by new { p.Maturity, p.PetId } into gp
                   select new { Maturity = gp.Key.Maturity, Count = gp.Coeunt() };
 var element= Petmaturity.OrderByDescending(s => s.Count).First()

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

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

发布评论

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

评论(1

£烟消云散 2024-11-13 00:26:04

假设您的方法采用 lambda:

Func<Pet, T> getData

尝试如下操作:

public Tuple<T, int> GetMostCommonProperty<T>(IEnumerable<Pet> pets,
                                              Func<Pet, T> getData)
{
    var petGroups = from p in pets
                    let data = getData(p)
                    where data != null
                    group p by new { Data = data, p.PetId } into gp
                    select new { Data = gp.Key.Data, Count = gp.Count() };
    var element = petGroups.OrderByDescending(s => s.Count).First();

    return Tuple.Create(element.Data, element.Count);
}

示例使用:

Tuple<string, int> mostCommonName = GetMostCommonProperty(PetsAgeDataTable,
                                                          pet => pet.Name);

请注意,您需要一个通用返回类型(除非它始终为 int)。你不能真正返回匿名类型,但你需要返回一些东西。这里我选择了一个元组,但是你有很多选择,这取决于你打算如何使用它。

Assuming your method takes a lambda:

Func<Pet, T> getData

Try something like:

public Tuple<T, int> GetMostCommonProperty<T>(IEnumerable<Pet> pets,
                                              Func<Pet, T> getData)
{
    var petGroups = from p in pets
                    let data = getData(p)
                    where data != null
                    group p by new { Data = data, p.PetId } into gp
                    select new { Data = gp.Key.Data, Count = gp.Count() };
    var element = petGroups.OrderByDescending(s => s.Count).First();

    return Tuple.Create(element.Data, element.Count);
}

Sample use:

Tuple<string, int> mostCommonName = GetMostCommonProperty(PetsAgeDataTable,
                                                          pet => pet.Name);

Note that you need a generic return type here (unless it's always int). You can't really return the anonymous type, but you need to return something. Here I chose a Tuple, but you have many options, depending on how you intend to use it.

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