简体   繁体   English

Lambda帮助在C#中进行分组和汇总,需要一个Func <IGrouping<int, anonymous type, int> 或表达 <Func<IGrouping<int, anonymous type> ,十进制&gt;&gt;

[英]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 :-) 我希望这有帮助 :-)

为什么 C# Func<int> 不能分配给 Func<object> ?<div id="text_translate"><p> 我在 C# 研究了协变和逆变。下面我的代码有一个错误。</p><pre> object a = new object(); int b = 10; a = b; // not error Func<object> acO = () => new object(); Func<int> acI = () => 1; acO = acI; // error</pre><p> 错误信息:</p><blockquote><p> 无法将类型“System.Func”隐式转换为类型“System.Func”。</p></blockquote><p> 我认为如果 int -> object 是可能的,那么 Func -> Func 将是可能的。 但事实并非如此。</p><p> 我认为值类型在返回时将被复制以使用,这与引用类型(对象)不同,并且它可能导致意外操作(如异常)。 我的猜测正确吗?</p><p> 我期待着您明智的回答。 感谢您阅读。</p></div></object></int> - Why C# Func<int> cannot be assigned to Func<object>?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c#-WPF:无法将异步lambda表达式转换为委托类型&#39;Func <int> &#39; - c#-WPF: Cannot convert async lambda expression to delegate type 'Func<int>' IGrouping <int, Keypair> 到字典 - IGrouping<int, Keypair> to Dictionary C#-将匿名类型解析为int32 - C# - Parse anonymous type to int32 迭代列表 <int> 以匿名类型 - Iterating List<int> in a Anonymous Type 使用Where(表达式 <Func<T, bool> &gt;)在IGrouping中 - Using Where( Expression<Func<T, bool>> ) in IGrouping 分组 <anonymous type:, string> 不包含“”的定义,也没有扩展方法“” - IGrouping<anonymous type:, string> does not contain a definition for '' and no extension method '' 通过功能 <int, int> 无需使用lambda表达式即可在C#中访问Task.Run() - Passing a Func<int, int> to Task.Run() in C# without using lambda expressions 为什么 C# Func<int> 不能分配给 Func<object> ?<div id="text_translate"><p> 我在 C# 研究了协变和逆变。下面我的代码有一个错误。</p><pre> object a = new object(); int b = 10; a = b; // not error Func<object> acO = () => new object(); Func<int> acI = () => 1; acO = acI; // error</pre><p> 错误信息:</p><blockquote><p> 无法将类型“System.Func”隐式转换为类型“System.Func”。</p></blockquote><p> 我认为如果 int -> object 是可能的,那么 Func -> Func 将是可能的。 但事实并非如此。</p><p> 我认为值类型在返回时将被复制以使用,这与引用类型(对象)不同,并且它可能导致意外操作(如异常)。 我的猜测正确吗?</p><p> 我期待着您明智的回答。 感谢您阅读。</p></div></object></int> - Why C# Func<int> cannot be assigned to Func<object>? C#-如何访问在Func中使用的特定类型的实例 <int> 代表 - C# - How to access instances of a particular type that are used in Func<int> delegate IGrouping 避免匿名类型 - IGrouping avoiding anonymous types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM