/LinqSelectManyExample

Comparing the 1st and 2nd overload of LINQ 'SelectMany' with LINQ Query Syntax.

Primary LanguageC#

Comparing LINQ Query Syntax with the
1st and 2nd overload of LINQ 'SelectMany'

Example of getting all variations of 3 characters from 3 strings using LINQ:

  1. with LINQ query syntax
  from x in "ABC"
  from y in "123"
  from z in "123"
  select $"{x}{y}{z} ");
  1. with LINQ method syntax, using the 1st overload of 'SelectMany'
    (only with collection selector, this needs an additional nested select)
  "ABC".SelectMany(x => "123".Select(y => $"{x}{y}"))
       .SelectMany(x => "123".Select(y => $"{x}{y} ")));
  1. with LINQ method syntax, using the 2nd overload of 'SelectMany'
    (with collection selector and result selector)
  "ABC".SelectMany(x => "123", (x, y) => $"{x}{y}")
       .SelectMany(x => "123", (x, y) => $"{x}{y} "));