Lambda帮助C#中的分组和求和,需要一个Func或表达式,小数>>

发布于 2024-09-11 05:18:19 字数 887 浏览 0 评论 0 原文

我在执行内联 .GroupBy() 和 .Sum() 操作时遇到一些麻烦。

我有通过 let 获得的数据子集,因此它的返回类型已经是匿名的。希望此示例中有足够的代码来展示我想要实现的目标...

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
   }

我需要一个有效的:

Func<IGrouping<int, anonymous type>, int> selector

Expression<Func<IGrouping<int, anonymous type>, decimal>> selector

I'm having a bit of trouble doing an inline .GroupBy() and .Sum() action.

I have a subset of data obtained via a let so it's return type is already anonymous. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见 2024-09-18 05:18:31

您需要将分组传递到 Sum 选择器中,如下

.Sum(g => ...)

所示这里“g”是您的分组。它有一个键,它是分组所依据的值,并且它还包含您分组的所有值。这些是您的匿名类型的值。

如果您在“g”上调用 Sum,它将为您提供为匿名类型指定选择器的选项。

//this is how you can sum values within a grouping
.Sum(g => g.Sum(tb => tb.Fee))

在我的示例中,我猜测“Fee”是属性名称,但是一旦取消引用“tb”变量,您的智能感知就会启动并显示可用的属性。

我希望这有帮助:-)

You need to pass the grouping into the Sum selector like this

.Sum(g => ...)

Here "g" is your grouping. 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.

//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.

I hope this helps :-)

恏ㄋ傷疤忘ㄋ疼 2024-09-18 05:18:29

看看你是否可以编写 .Sum(x => x.Key.Fee),但我怀疑整个查询是否会起作用或返回预期结果......

See if you can write .Sum(x => x.Key.Fee), but I doubt that the whole query will work or returns the expected result...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文