我有一个带有列
的表结构FeesNormal
FeesCustom
货币
现在我正在寻找按货币计算的SUM功能组。
例如20 USD + 30 EURO + 40 INR此表中的内容
如果FeesCustom>我还必须考虑这个场景。 0我必须忽略行的FeesNormal
样本日期和预期结果是这样的
[2016-05-30 08:16:00.934 UTC] Large Lookup Fast Random UInt: 273 ms
[2016-05-30 08:16:01.230 UTC] Large Lookup Fast Random: 294 ms
[2016-05-30 08:16:01.503 UTC] Large Lookup Optimized Random: 273 ms
[2016-05-30 08:16:01.837 UTC] Fastest Optimized Random Modded: 333 ms
[2016-05-30 08:16:02.245 UTC] Numbers: 408 ms
[2016-05-30 08:16:02.532 UTC] Large Lookup Parameterless Random: 287 ms
[2016-05-30 08:16:02.816 UTC] Large Lookup: 284 ms
[2016-05-30 08:16:03.145 UTC] Lookup Optimized Modded: 329 ms
[2016-05-30 08:16:03.486 UTC] Fastest Optimized Modded: 340 ms
[2016-05-30 08:16:03.824 UTC] Optimized Modded Const: 337 ms
[2016-05-30 08:16:04.154 UTC] Optimized Modded: 330 ms
[2016-05-30 08:16:04.524 UTC] Modded: 370 ms
[2016-05-30 08:16:05.700 UTC] Simple: 1176 ms
[2016-05-30 08:16:07.309 UTC] Another simple with HashSet: 1609 ms
[2016-05-30 08:16:09.774 UTC] Another Simple: 2465 ms
[2016-05-30 08:16:17.450 UTC] Option (Compiled) Regex: 7675 ms
[2016-05-30 08:16:34.090 UTC] Regex: 16640 ms
[2016-05-30 08:16:54.793 UTC] EndsWith: 20702 ms
我能够使用linq找到总和
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 EUR
Expected result 35 USD 20 EUR
答案 0 :(得分:20)
在我看来,你只需要一个从“入场”到“有效费用”的预测,你可以总结 - 例如:
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,
(key, values) => new { Currency = key, Fee = values.Sum() });
答案 1 :(得分:2)
使用查询语法:
var feeResult = (from fee in fee_list
group fee by fee.Currency into groupResult
select new
{
Currency = groupResult.Key,
FinalFees = groupResult.Sum(f => f.FeesCustom > 0 ? f.FeesCustom : f.FeesNormal)
}).ToList();
答案 2 :(得分:1)
假设您有DataTable
所提及的数据,您可以使用Linq
var result = table.AsEnumerable()
.GroupBy(x=> x.Field<string>("Currency"))
.Select(x=> new
{
Currency = x.Key,
Value = x.Sum(s=> Math.Max(s.Field<double>("FeesNormal"), s.Field<double>("FeesCustom "))),
}
.ToList()