GroupBy的2列表的并集 [英] Union of 2 list by GroupBy

查看:130
本文介绍了GroupBy的2列表的并集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个带有1列货币"和总计"的列表,例如

I have two lists with 1 columns Currency and Total like

    list1
      Currency  Total
      USD       10         
      EUR       25  

list2
      Currency  Total
      USD       10         
      EUR       25 
      INR       55

如何从这样按货币分组的单个列表中的两个列表中获得总和

How can i get an overall Sum from the two lists in a single list grouped on currency basis like this

ListSummary

      Currency Total
      USD       20
      EUR       50
      INR       55 

以下Linq代码为我生成了2个列表

The following Linq code is generating the 2 lists for me

         var license_sum = licenses.GroupBy(x => x.Currency,
                        (key, values) => new {
                            Currency = key,
                            Total = values.Sum(x => x.FeesCustom == 0 ? x.FeesNormal : x.FeesCustom)
                        });
        var trans_sum = translations.GroupBy(x => x.Currency,
                        (key, values) => new {
                            Currency = key,
                            Total = values.Sum(x => x.FeesCustom == 0 ? x.FeesNormal : x.FeesCustom)
                        });

从这2个中,我正在计划overool_sum列表

From these 2 i am planning for overool_sum list

推荐答案

您可以使用Concat linq扩展名合并两个列表,然后使用GroupBy进行分组..

You could use Concat linq extension to concat both lists and then use GroupBy to group on currecy..

var overool_sum = license_sum.Concat(trans_sum )
                          .GroupBy(x => x.Currency)
                          .Select(x=>  new 
                          {
                            Currency = x.Key,
                            Total = x.Sum(x => x.Total)
                          })
                          .ToList();

这篇关于GroupBy的2列表的并集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆