2015-09-25 94 views
1
int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; 
int[] numbersB = { 1, 3, 5, 7, 8 }; 

var pairs = 
    from a in numbersA 
    from b in numbersB 
    where a < b 
    select new { a, b }; 
+0

https://social.msdn.microsoft.com/Forums/en-US/190e1fdf-0b38-4d30-ac52-59c8d15f771a/c-possible-lambda-in-arrays-content-declaration?forum=csharplanguage – kevintjuh93

+0

possible [使用lambda类型的Init数组的副本](http://stackoverflow.com/questions/5368492/init-array-of-type-with-lambda) – kevintjuh93

+1

请参阅http://codeblog.jonskeet.uk/2011/01/28/reimplementing-linq-to-objects-part-41-how-query-expressions-work /查询表达式如何翻译。 –

回答

3

你可以尝试这样的事:

var pairs = numbersA.SelectMany(a => numbersB.Where(b => b>a) 
        .Select(b => new { a, b })); 

请参阅本.NET Fiddle

什么SelectMany

将序列的每个元素投影到IEnumerable并将产生的序列展平成一个序列。

SelectMany的结果是将包含 numbersB阵列,其比 a更大所有当前 a和所有的数字之间的组合

所以,我们选择一个匿名类型具有两个属性,ab 。对numbersA中的所有数字执行此操作,我们就会得到我们想要的。