查询与GROUPBY [英] lookup vs. groupby

查看:133
本文介绍了查询与GROUPBY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道有什么GROUPBY和ToLookup扩展方法之间的差异。

I'm wondering what's the difference between the groupBy and the ToLookup Extension Method.

让我们有这样的对象的列表:

Let us have a List of objects like this:

public class Person
{
    public uint Id { get; set; }
    public string Name { get; set; }
    public DateTime Birthday { get; set; }
}


List<Person> People { get; set; }

现在我可以使用扩展方法上面:

Now i can use the Extension Methods above:

var groupedPeople = People.GroupBy((x) => x.Id);

var lookupPeople = People.ToLookup((x) => x.Id);

什么是这些语句之间的区别是什么?

What's the difference between those statements?

在此先感谢。

马湾

推荐答案

ToLookup 使用的直接的执行,并返回一个 ILookup ,允许您通过关键要看组了。

ToLookup uses immediate execution, and returns an ILookup which allows you to look the groups up by key.

GROUPBY 使用的延迟的执行,只是返回组中,每个组首先出现的顺序(所以第一个组将包含源数据的第一元素,例如),与不知道能够通过键后看的基团上。每次迭代的结果,将不得不重新组

GroupBy uses deferred execution, and just returns you groups in the order in which each group was first encountered (so the first group will contain the first element of the source data, for example), with no idea of being able to look the groups up later by key. Each time you iterate over the results, it will have to group again.

基本上,你应该使用取决于你打算做什么用的结果。如果你只是去遍历他们一个单一的时间(例如,用于进一步的转换), GROUPBY 通常是罚款。如果你想保持其作为多个操作的集合, ToLookup 的即时性是​​非常有用的。

Basically, which you should use depends on what you're going to do with the results. If you're just going to iterate over them a single time (e.g. for further transformation), GroupBy is usually fine. If you want to keep them as a collection for multiple operations, the immediate nature of ToLookup is useful.

这篇关于查询与GROUPBY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆