var result = list.GroupBy(t => t.Id).CartesianProduct();
使用Eric Lippert 博客中的CartesianProduct 扩展方法:
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 list = new[]
{
new { Id = 1, Description = "Red" },
new { Id = 1, Description = "Blue" },
new { Id = 1, Description = "Green" },
new { Id = 2, Description = "Small" },
new { Id = 2, Description = "Med" },
new { Id = 2, Description = "Large" },
new { Id = 3, Description = "Cotton" },
new { Id = 3, Description = "Silk" },
};
var result = list.GroupBy(t => t.Id).CartesianProduct();
foreach (var item in result)
{
Console.WriteLine(string.Join(" ", item.Select(x => x.Description)));
}
输出:
Red Small Cotton
Red Small Silk
Red Med Cotton
Red Med Silk
Red Large Cotton
Red Large Silk
Blue Small Cotton
Blue Small Silk
Blue Med Cotton
Blue Med Silk
Blue Large Cotton
Blue Large Silk
Green Small Cotton
Green Small Silk
Green Med Cotton
Green Med Silk
Green Large Cotton
Green Large Silk