下面的程序生成排列有什么问题?

时间:2011-10-13 05:55:25

标签: c# linq

 var query = from p1 in Enumerable.Range(2,3)
                        from p2 in Enumerable.Range(4,5)                       
                        select new { p1 , p2}; 

结果似乎是错误的

{ p1 = 2, p2 = 4 }
{ p1 = 2, p2 = 5 }
{ p1 = 2, p2 = 6 }
{ p1 = 2, p2 = 7 }
{ p1 = 2, p2 = 8 }
{ p1 = 3, p2 = 4 }
{ p1 = 3, p2 = 5 }
{ p1 = 3, p2 = 6 }
{ p1 = 3, p2 = 7 }
{ p1 = 3, p2 = 8 }
{ p1 = 4, p2 = 4 }
{ p1 = 4, p2 = 5 }
{ p1 = 4, p2 = 6 }
{ p1 = 4, p2 = 7 }
{ p1 = 4, p2 = 8 }

需要帮助

1 个答案:

答案 0 :(得分:3)

我想你希望结果是{2,4},{2,5},{3,4},{3,5}。如果是这种情况,也许您错过了Enumerable.Range的第二个参数不是上限 - 它是一个长度。在这种情况下,你想要:

var query = from p1 in Enumerable.Range(2, 2)
            from p2 in Enumerable.Range(4, 2)
            select new { p1 , p2};