w.b
w.b

Reputation: 11228

LINQ query in lambda syntax

What would be the proper way to write this query with lambda syntax?

var palindromes = from i in Enumerable.Range(100, 9900)
                  from j in Enumerable.Range(100, 9900)
                  let product = (i * j)
                  where product.ToString() == new string(product.ToString().Reverse().ToArray())
                  orderby product
                  select product;

Upvotes: 0

Views: 100

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

var palindromes = Enumerable.Range(100, 9900)
    .SelectMany(
        i => Enumerable.Range(100, 9900),
        (i, j) => i * j)
    .Where(p => /* where condition */)
    .OrderBy(p => p);

That's not exactly how compiler will transform your query, but result should be the same.

You can check rules compiler follows when transforming syntax query to method invokaction in c# specification.

Upvotes: 2

Related Questions