在c#中使用LINQ查找分组列表的组合

时间:2012-05-30 19:54:26

标签: c# linq

假设我有一个包含人名及其原籍城市的对象。

public class personDetails
{
    public string City;
    public string Name;
}

我有一个列表,其中添加了以下条目。

Name   City
John | London
Jane | London
Tom  | New York
Bob  | New York
Fred | New York

我正在寻找的是所有可能的名称组合,按城市分组。

John Tom
John Bob  
John Fred
Jane Tom
Jane Bob
Jane Fred

如果我事先知道组的数量,我可以使用以下代码

List<personDetails> personList = new List<personDetails>();
//populate list

var groupedPersons = personList.GroupBy(c => c.City);
foreach (var item1 in groupedPersons[0])
{
    foreach (var item2 in groupedPersons[1])
    {
        Console.WriteLine(item1.Name + " " + item2.Name);
    }          
}

但是,这只有在事先知道群组数量的情况下才有效,并随着群组数量的增加而变得笨拙。我确信有一种优雅的方法可以使用LINQ做到这一点,任何人都可以解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

我们将从以下here的verbatum代码片段开始。 (这是一个很好的链接,值得一读)。

public static class MyExtensions
{
    public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
    {
        IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
        return sequences.Aggregate(
            emptyProduct,
            (accumulator, sequence) =>
            from accseq in accumulator
            from item in sequence
            select accseq.Concat(new[] { item }));
    }
}

之后我们需要做的就是:

var groupedPersons = personList.GroupBy(c => c.City)
    //need an enumerable of enumerables, not an enumerable of groupings, 
    //because the method isn't covariant.
    .Select(group => group.AsEnumerable());

var results = groupedPersons.CartesianProduct();
foreach (var group in results)
{
    foreach (var person in group)
    {
        Console.Write(person.Name + " ");
    }
    System.Console.WriteLine();
}