robertz
robertz

Reputation: 778

Multiple from clauses in LINQ

How can this LINQ query expression be re-expressed with extension method 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();
}

Upvotes: 1

Views: 2501

Answers (2)

jason
jason

Reputation: 241661

Enumerable.Range(1, 3).SelectMany(
    i => Enumerable.Range(4, 3),
    (i, j) => new Tuple<int, int>(i, j)
).ToList();

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038940

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 :-)

Upvotes: 10

Related Questions