如何使用linq压缩以下数据。
这里我想按Column1分组,Column2和Column3是字典的键和值
Pete, small, 2.0
Pete, medium, 3.5
Sbux, small, 2.5
Sbux, medium, 3.0
Mcd, large, 3.0
Mcd, refill, 5.0
Dd, refill, 4.0
Dd, solo, 2.0
cb, xlarge, 4.0
cb, premium, 5.0
Sb, premium, 4.0
Sb, xlarge,refill,extra, 7.0
以下 -
Pete, small, 2.0, medium, 3.5
Sbux, small, 2.5, medium, 3.0
Mcd, large, 3.0, refill, 5.0
Dd, refill, 4.0, solo, 2.0
Cb, xlarge, 4.0, premium, 4.0
Sb, premium, 4.0, xlarge,refill,extra, 7.0
答案 0 :(得分:2)
假设Column2
组中的Column1
值不重复,解决方法如下:
var flat = data
.GroupBy(item => item.Column1)
.Select(g => new {
Column1 = g.Key
, Dict = g.ToDictionary(r => r.Column2, r => r.Column3)
}).ToList();