开发者

Getting sub list

From the given set

{1,2开发者_高级运维,3,4,5,6,7,8,99,89}

What is the way to get all possible two numbers subset using LINQ?

(i.e) {1,2},{1,3},{1,4} .....


cross join?

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);
}

For triples:

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);
}


This link doesn't support LINQ queries, but I consider it related and relevant: Permutations, Combinations, and Variations using C# Generics

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜