GROUPBY在lambda表达式

编程入门 行业动态 更新时间:2024-10-24 22:29:52
本文介绍了GROUPBY在lambda表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 from x in myCollection group x by x.Id into y select new { Id = y.Key, Quantity = y.Sum(x => x.Quantity) };

您会怎么写上面Lambda表达式?我卡上的组到部分。

How would you write the above as a lambda expression? I'm stuck on the group into part.

推荐答案

查询延续(SELECT ... INTO和组...成,但的不的加入...进入)等价于刚分手的查询表达式。所以我喜欢把你的例子为:通过X从X MyCollection中组X

Query continuations (select...into and group...into, but not join...into) are equivalent to just splitting up the query expression. So I like to think of your example as:

var tmp = from x in myCollection group x by x.Id; var result = from y in tmp select new { Id = y.Key, Quantity = y.Sum(x => x.Quantity) };

改变那些进入点符号:

Change those into dot notation:

var tmp = myCollection.GroupBy(x => x.Id); var result = tmp.Select(y => new { Id = y.Key, Quantity = y.Sum(x => x.Quantity) });

然后,你可以将它们合并回:

Then you could combine them back:

var tmp = myCollection.GroupBy(x => x.Id) .Select(y => new { Id = y.Key, Quantity = y.Sum(x => x.Quantity) });

一旦你制定出C#编译器与查询表达式是什么,其余的是相对简单的:)

Once you work out what the C# compiler does with query expressions, the rest is relatively straightforward :)

更多推荐

GROUPBY在lambda表达式

本文发布于:2023-10-30 12:00:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1542787.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   GROUPBY   lambda

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!