本文介绍了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表达式
发布评论