[英]Lambda Help Grouping and Summing in C#, need a Func<IGrouping<int, anonymous type, int> or Expression<Func<IGrouping<int, anonymous type>, decimal>>
I'm having a bit of trouble doing an inline .GroupBy() and .Sum() action. 我在执行内联.GroupBy()和.Sum()操作时遇到了一些麻烦。
I have a subset of data obtained via a let so it's return type is already anonymous. 我有一个通过let获得的数据子集,所以它的返回类型已经是匿名的。 Hopefully there's enough code in this sample to show what I'm trying to achieve...
希望此示例中有足够的代码来显示我要实现的目标...
from r in Repo.R
join f in Repo.F
on f.ID equals r.FFK
let totalB = Repo.B
.Join(
b => b.FKID,
f => f.ID
(b, f) => new { b.ID, b.Qty, b.Fee })
.Where(
b => b.someCriteria == someInput)
group r by new
{
r.Name,
TotalFee = totalB
.GroupBy(tb => tb.TypeId)
.Sum( /*having trouble here*/ )
}
into rgroup
select new FinalOutput
{
rgroup.Key.Name,
rgroup.Key.TotalFee
}
I need a valid: 我需要一个有效的:
Func<IGrouping<int, anonymous type>, int> selector
or 要么
Expression<Func<IGrouping<int, anonymous type>, decimal>> selector
看看是否可以编写.Sum(x => x.Key.Fee)
,但是我怀疑整个查询是否可以正常工作或返回预期的结果...
You need to pass the grouping into the Sum selector like this 您需要像这样将分组传递到Sum选择器中
.Sum(g => ...)
Here "g" is your grouping. 这里的“ g”是您的分组。 It has a key which is the value that it was grouped by, and it also contains all of the values that you grouped.
它具有一个键,该键是分组的值,并且还包含您分组的所有值。 These are values of your anonymous type.
这些是您的匿名类型的值。
If you call Sum on "g" it'll give you the option of specifying a selector for your anonymous type. 如果在“ g”上调用Sum,它将为您提供为匿名类型指定选择器的选项。
//this is how you can sum values within a grouping
.Sum(g => g.Sum(tb => tb.Fee))
I've guessed "Fee" as the property name in my example, but once you de-reference the "tb" variable your intellisense should kick in and show you the properties available. 在我的示例中,我已经猜想“ Fee”为属性名称,但是一旦取消引用“ tb”变量,您的intellisense应该会显示出来并向您显示可用的属性。
I hope this helps :-) 我希望这有帮助 :-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.