# 两个集合中的数据，怎样统计到一个比较的集合中？

0

List<PC> current

ID area type  total  year

----------------------------

1  广州 CPU  10000  2016

2  深圳 显卡  500    2016

List<PC> last

ID area  type    total  year

-------------------------------

1  广州  CPU    20000  2015

2  深圳 显卡     600     2015

3  东莞  SSD    200   2015

area   type    2016      2015

--------------------------------

sidecore | 初学一级 | 园豆：2

0
```using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
var lstA1 = new List<A>();
var lstA2 = new List<A>();

var query = from a1 in lstA1
join a2 in lstA2 on new { a1.Area, a1.Type } equals new { a2.Area, a2.Type }
select new B
{
Area = a1.Area,
Type = a1.Type,
TotalA = a1.Total,
TotalB = a2.Total
};

var lstB = query.ToList();
}
}

class A
{
public string Area { get; set; }

public string Type { get; set; }

public int Total { get; set; }

public int Year { get; set; }
}

class B
{
public string Area { get; set; }

public string Type { get; set; }

/// <summary>
/// 2015年total
/// </summary>
public int TotalA { get; set; }

/// <summary>
/// 2016年total
/// </summary>
public int TotalB { get; set; }
}
}```
Gamain | 园豆：357 (菜鸟二级) | 2016-01-07 11:32

@sidecore: 用linq的左连接，由于linq没有右链接(貌似)，而且全连接实现比较复杂。

0
```IEnumerable<pc> all = current.Concat(last);
var aresult = from al in all
group al by new { al.area, al.type } into ty
select new
{
area = ty.Key.area,
type = ty.Key.type,
cy = ty.Where(c => c.year == 2016).Sum(c => c.total),
ly = ty.Where(c => c.year == 2015).Sum(c => c.total)
};```

您需要登录以后才能回答，未注册用户请先注册