Multiple from clauses in LINQ
How can this LINQ query expression be re-expressed with extension meth开发者_Python百科od calls?
public static List<Tuple<int, int>> Concat()
{
return (from x in Enumerable.Range(1, 3)
from y in Enumerable.Range(4, 3)
select new Tuple<int, int>(x, y)).ToList();
}
return Enumerable.Range(1, 3).SelectMany(x => Enumerable.Range(4, 3)
.Select(y => new Tuple<int, int>(x, y))).ToList();
Your version looks more readable :-)
Enumerable.Range(1, 3).SelectMany(
i => Enumerable.Range(4, 3),
(i, j) => new Tuple<int, int>(i, j)
).ToList();
精彩评论