【发布时间】:2015-09-29 12:56:04
【问题描述】:
我有一张发票清单以及每张发票上的所有产品。 每个发票可以有多个相同的产品
class InvoiceProducts
{
public int InvoiceID { get; set; }
public int ProductID { get; set; }
}
var list = new List<InvoiceProducts>();
list.Add(new { InvoiceID = 7000, ProductID=15});
list.Add(new { InvoiceID = 7000, ProductID=10});
list.Add(new { InvoiceID = 7000, ProductID=10});
list.Add(new { InvoiceID = 7000, ProductID=15});
list.Add(new { InvoiceID = 7010, ProductID=12});
list.Add(new { InvoiceID = 7010, ProductID=20});
list.Add(new { InvoiceID = 7010, ProductID=12});
list.Add(new { InvoiceID = 7021, ProductID=1});
list.Add(new { InvoiceID = 7021, ProductID=1});
我可以请求帮助吗? 按 InvoiceID 分组,并具有唯一产品的(排序的)整数列表 每张发票 (排序的原因是我以后需要把这个和其他相同产品的发票匹配)
即
InvoiceID ProductID
7000 10,15
7010 12,20
7021 1
尝试失败:
var tl2 = List
.GroupBy(x => x.InvoiceID)
.ToDictionary(y => y.Key, y => y.Distinct().ToList());
失败的尝试解释:它有一个按 InvoiceID 正确分组的字典,但发票 7000 有 4 个行项目而不是 2 个唯一产品
【问题讨论】:
标签: c# linq unique linq-group