在我previous question上收到的帮助中,我有一个working linq查询,其中包含InvoiceID +每个发票的产品列表:
我可以成功循环查找(如代码所示),并显示密钥, 但是我需要帮助才能显示每个键的元素列表。
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class Program
{
class InvoiceProducts
{
public int InvoiceID { get; set; }
public int ProductID { get; set; }
}
public static void Main()
{
List<InvoiceProducts> list = new List<InvoiceProducts>();
list.Add(new InvoiceProducts{ InvoiceID = 7000, ProductID=15});
list.Add(new InvoiceProducts{ InvoiceID = 7000, ProductID=10});
list.Add(new InvoiceProducts{ InvoiceID = 7000, ProductID=10});
list.Add(new InvoiceProducts{ InvoiceID = 7000, ProductID=15});
list.Add(new InvoiceProducts{ InvoiceID = 7010, ProductID=12});
list.Add(new InvoiceProducts{ InvoiceID = 7010, ProductID=20});
list.Add(new InvoiceProducts{ InvoiceID = 7010, ProductID=12});
list.Add(new InvoiceProducts{ InvoiceID = 7021, ProductID=1});
list.Add(new InvoiceProducts{ InvoiceID = 7021, ProductID=1});
var lookup = list.Select(x => new { x.InvoiceID, x.ProductID })
.Distinct()
.ToLookup(x => x.InvoiceID, x => x.ProductID);
foreach(var x in lookup)
{
Console.WriteLine(x.Key);
}
}
}
应该返回:
7000 10,15
7010 12,20
7021 1
答案 0 :(得分:1)
这应该有效: -
var result = list.GroupBy(x => x.InvoiceID)
.Select(x => x.Key + "," +
String.Join(",", x.Select(z => z.ProductID).Distinct()));
答案 1 :(得分:1)
您只需要枚举error setting certificate verify locations:
CAfile: /tmp/cert.cer
CApath: none
这是x
,您可以使用IEnumerable<int>
根据需要连接有序的String.Join
:
int
您还可以创建类似foreach(var x in lookup)
{
Console.WriteLine("{0} {1}", x.Key, String.Join(",", x.OrderBy(i => i)));
}
的集合:
List<int>