你能解释一下这个拉姆达分组功能? [英] Can you explain this lambda grouping function?

查看:346
本文介绍了你能解释一下这个拉姆达分组功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用LINQ和 LAMBDA防爆pressions 一段时间,但我还没有完全的舒适与功能的各个方面。

所以,当我工作的一个项目最近我需要得到基于了一些财产的对象不同的列表,我跑过这个code。它的工作原理,和我很好,但我想了解的分组机制。我不喜欢简单地插入code并逃离问题,如果我能帮助它。

反正在code是:

  VAR listDistinct
    = list.GroupBy(
        I => i.value1,
        (重点,组)=> group.First()
    ).ToList();
 

在上面的code样品,你第一次叫 GROUPBY ,并通过它的lambda EX pression告诉它组由酒店值1 。的code第二部分是造成混乱。

据我所知,所引用值1 (按键,组)的声明,但我还是不包装我的头周围的所有内容到正在发生。

解决方案
  

这是什么除权pression

  list.GroupBy(
    I => i.value1,
    (重点,组)=> group.First())
 

     

吗?

这将创建一个查询,它在执行时,分析序列列表制作组的序列,然后的项目的组的序列成一个新的序列。在这种情况下,突起是采取的第一项出各组的。

第一拉姆达选择钥匙在其上组构成。在这种情况下,列表中的所有项目具有相同值1 属性都放在一个组。它们共享的价值成为了该组的钥匙。

这键控组的序列中的第二拉姆达项目;就好像你在组的顺序做了选择。这个查询的净效果是选择一组从列表中的元素,使得所得序列的每个元素具有值1的一个不同的值属性

该文档是在这里:

http://msdn.microsoft.com/en-us/library/ bb549393.aspx

如果该文件是不明确的,我很高兴地批评一起传递到文件管理器。

  

这code使用作为拉姆达的形式参数。是不是 A保留关键字?

没有,上下文关键字的。 LINQ加入到C#3.0,所以有可能已使用作为标识符现有的方案。如果重新编译被做了保留关键字时,这些方案将被打破。相反,仅在查询前pression上下文关键字。一个查询前pression外面是普通的标识符。

如果你想提请注意一个事实,即它是一个普通的标识符,或者如果你想使用的标识符内部查询EX pression,你可以告诉编译器把它当作一个标识符,而不是关键字由$ P $与 pfacing它@ 。被我写code以上我想说

  list.GroupBy(
    I => i.value1,
    (键,@group)=> @ group.First())
 

要分清楚。

  

有没有在C#中的其他上下文关键字?

是的。我已经证明他们都在这里:

http://ericlippert.com/2009/05/11 /保留,和语境的-关键字/

I've been using LINQ and Lambda Expressions for a while, but I'm still not completely comfortable with every aspect of the feature.

So, while I was working on a project recently I needed to get a distinct list of objects based off of some property, and I ran across this code. It works, and I'm fine with that, but I'd like to understand the grouping mechanism. I don't like simply plugging code in and running away from the problem if I can help it.

Anyways the code is:

var listDistinct
    =list.GroupBy(
        i => i.value1,
        (key, group) => group.First()
    ).ToList();

In the code sample above, you're first calling GroupBy and passing it a lambda expression telling it to group by the property value1. The second section of the code is causing the confusion.

I understand that key is referencing value1 in the (key, group) statement, but I'm still not wrapping my head around everything that's taking place.

解决方案

What does the expression

list.GroupBy(
    i => i.value1,
    (key, group) => group.First())

do?

This creates a query which, when executed, analyzes the sequence list to produce a sequence of groups, and then projects the sequence of groups into a new sequence. In this case, the projection is to take the first item out of each group.

The first lambda chooses the "key" upon which the groups are constructed. In this case, all items in the list which have the same value1 property are put in a group. The value that they share becomes the "key" of the group.

The second lambda projects from the sequence of keyed groups; it's as though you'd done a select on the sequence of groups. The net effect of this query is to choose a set of elements from the list such that each element of the resulting sequence has a different value of the value1 property.

The documentation is here:

http://msdn.microsoft.com/en-us/library/bb549393.aspx

If the documentation is not clear, I am happy to pass along criticisms to the documentation manager.

This code uses group as the formal parameter of a lambda. Isn't group a reserved keyword?

No, group is a contextual keyword. LINQ was added to C# 3.0, so there might have already been existing programs using group as an identifier. These programs would be broken when recompiled if group was made a reserved keyword. Instead, group is a keyword only in the context of a query expression. Outside of a query expression it is an ordinary identifier.

If you want to call attention to the fact that it is an ordinary identifier, or if you want to use the identifier group inside a query expression, you can tell the compiler "treat this as an identifier, not a keyword" by prefacing it with @. Were I writing the code above I would say

list.GroupBy(
    i => i.value1,
    (key, @group) => @group.First())

to make it clear.

Are there other contextual keywords in C#?

Yes. I've documented them all here:

http://ericlippert.com/2009/05/11/reserved-and-contextual-keywords/

这篇关于你能解释一下这个拉姆达分组功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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