Linq to dataset 根据列的最大值选择行

发布于 2024-09-15 03:36:04 字数 122 浏览 3 评论 0原文

我有一个数据集表,我想按列MOID对其进行分组,然后在该组中我想选择具有列radi最大值的行。

谁能告诉我如何通过 LINQ to dataset 来做到这一点?

I have a dataset table, I want to group it by column MOID, and then within this group I want to select the row which has max value of column radi.

Can anybody show me how to do it via LINQ to dataset?

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

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

发布评论

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

评论(3

从来不烧饼 2024-09-22 03:36:04

尽管 Barry 发布的解决方案应该可以工作(需要进行一些修复),但它并不是最优的:您不需要对集合进行排序来查找具有字段最大值的项目。我编写了一个 WithMax 扩展方法,它返回具有指定函数最大值的项目:

    public static T WithMax<T, TValue>(this IEnumerable<T> source, Func<T, TValue> selector)
    {
        var max = default(TValue);
        var withMax = default(T);
        bool first = true;
        var comparer = Comparer<TValue>.Default;
        foreach (var item in source)
        {
            var value = selector(item);
            int compare = comparer.Compare(value, max);

            if (compare > 0 || first)
            {
                max = value;
                withMax = item;
            }
            first = false;
        }
        return withMax;
    }

它仅迭代集合一次,这比仅对它进行排序以获取第一项要快得多。

然后您可以按如下方式使用它

var query =
    from row in table.AsEnumerable()
    group row by row.Field<int>("MOID") into g
    select g.WithMax(r => r.Field<int>("radi"));

Although the solution posted by Barry should work (with a few fixes), it is sub-optimal : you don't need to sort a collection to find the item with the maximum value of a field. I wrote a WithMax extension method, which returns the item with the maximum value of the specified function :

    public static T WithMax<T, TValue>(this IEnumerable<T> source, Func<T, TValue> selector)
    {
        var max = default(TValue);
        var withMax = default(T);
        bool first = true;
        var comparer = Comparer<TValue>.Default;
        foreach (var item in source)
        {
            var value = selector(item);
            int compare = comparer.Compare(value, max);

            if (compare > 0 || first)
            {
                max = value;
                withMax = item;
            }
            first = false;
        }
        return withMax;
    }

It iterates the collection only once, which is much faster than sorting it just to get the first item.

You can then use it as follows

var query =
    from row in table.AsEnumerable()
    group row by row.Field<int>("MOID") into g
    select g.WithMax(r => r.Field<int>("radi"));
宛菡 2024-09-22 03:36:04

这是未经测试的,但我认为这样的东西应该有效:

        var qry = from m in [YourDataSource]
                      group p by m.MOID into grp
                      select grp.OrderByDescending(a => a.RADI).First();

This is untested but I think something like this should work:

        var qry = from m in [YourDataSource]
                      group p by m.MOID into grp
                      select grp.OrderByDescending(a => a.RADI).First();
扎心 2024-09-22 03:36:04

这适用于一个查询!

public static T WithMax<T, TValue>(this IEnumerable<T> source, Func<T, TValue> keySelector)
{
    return source.OrderByDescending(keySelector).FirstOrDefault();
}

this works with one query!

public static T WithMax<T, TValue>(this IEnumerable<T> source, Func<T, TValue> keySelector)
{
    return source.OrderByDescending(keySelector).FirstOrDefault();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文