循环查找 - 将元素串在一起
Loop Lookup - string elements together
在 , I have a 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);
}
}
}
应该return:
7000 10,15
7010 12,20
7021 1
这应该有效:-
var result = list.GroupBy(x => x.InvoiceID)
.Select(x => x.Key + "," +
String.Join(",", x.Select(z => z.ProductID).Distinct()));
您只需要枚举 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>
:
foreach(var x in lookup)
{
List<int> productList = x.ToList();
}
在
我可以成功循环查找(如代码所示),并显示密钥, 但我需要帮助显示每个键的元素列表。
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);
}
}
}
应该return:
7000 10,15
7010 12,20
7021 1
这应该有效:-
var result = list.GroupBy(x => x.InvoiceID)
.Select(x => x.Key + "," +
String.Join(",", x.Select(z => z.ProductID).Distinct()));
您只需要枚举 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>
:
foreach(var x in lookup)
{
List<int> productList = x.ToList();
}