c# – Linq Sum with group by

c# – Linq Sum with group by,第1张

概述我有一个带有列的表结构 FeesNormal FeesCustom 货币 现在我正在寻找按货币计算的SUM功能组. 例如20美元30欧元40 INR来自此表 如果FeesCustom>我还必须考虑这个场景. 0我必须忽略该行的FeesNormal 样本日期和预期结果是这样的 FeesNormal FeesCustom Currency 10 0 US 我有一个带有列的表结构

Feesnormal

FeesCustom

货币

现在我正在寻找按货币计算的SUM功能组.

例如20美元30欧元40 INR来自此表

如果FeesCustom>我还必须考虑这个场景. 0我必须忽略该行的Feesnormal

样本日期和预期结果是这样的

Feesnormal  FeesCustom  Currency  10          0            USD     15          25           USD //in this case can ignore Feesnormal Since FeesCustom is more  5           10           EUR //same for this row ignore Feesnormal  10          0            EURExpected result  35 USD 20 EUR

我能够使用linq找到总和

int sum_custom=(int)fee_List.Where(p => p.FeesCustom > 0).Sum(p => p.FeesCustom); int sum_normal = (int)fee_List.Where(p => p.FeesCustom ==0).Sum(p => p.Feesnormal);
解决方法 在我看来,你只需要从“入门”到“有效费用”的预测,你可以总结 – 如:
var result = source    .GroupBy(x => x.Currency)    .Select(g => new {        Currency = g.Key,Total = g.Sum(x => x.FeesCustom > 0 ? x.FeesCustom : x.Feesnormal)    });

这相当于:

var result = source    .GroupBy(x => x.Currency,(key,values) => new {                Currency = key,Total = values.Sum(x => x.FeesCustom > 0 ? x.FeesCustom : x.Feesnormal)             });

或者先进行转换:

var result = source     .Select(x => new {         x.Currency,x.Fee = x => x.FeesCustom > 0 ? x.FeesCustom : x.Feesnormal     })     .GroupBy(x => x.Currency,x => x.Fee,values) => new { Currency = key,Fee = values.Sum() });
总结

以上是内存溢出为你收集整理的c# – Linq Sum with group by全部内容,希望文章能够帮你解决c# – Linq Sum with group by所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1239601.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存