获取子列表

时间:2009-10-18 20:56:27

标签: c# linq

从给定的集合

{1,2,3,4,5,6,7,8,99,89}

使用LINQ获取所有可能的两个数字子集的方法是什么?

(即){1,2},{1,3},{1,4} .....

2 个答案:

答案 0 :(得分:6)

交叉加入?

var data = new[] {1,2,3,4,5,6,7,8,99,89};
var qry = from x in data
          from y in data
          where x < y
          select new {x,y};
foreach (var pair in qry) {
    Console.WriteLine("{0} {1}", pair.x, pair.y);
}

三倍:

var data = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 99, 89 };
var qry = from x in data
          from y in data
          where x < y
          from z in data
          where y < z
          select new { x, y, z };
foreach (var tuple in qry) {
    Console.WriteLine("{0} {1} {2}", tuple.x, tuple.y, tuple.z);
}

答案 1 :(得分:1)

此链接不支持LINQ查询,但我认为它相关且相关:Permutations, Combinations, and Variations using C# Generics