5

I'm trying to use an array of named tuples in a LINQ (LINQ-to-object) query like this:

(int from, int to)[] inputRanges = { (1,2), (3,4) };

var result = from range in inputRanges
             select Enumerable.Range(range.from, range.to - range.from + 1);
return result.SelectMany(x => x);

However, I receive a compiler error, telling me that range has no member from and it instead expected a comma , instead of .from.

What am I doing wrong? Are named tuples and LINQ-to-objects not combinable?

1
  • If I use .Item1 and .Item2 instead it works - are named tuples a problem for LINQ? Why? Commented Nov 15, 2018 at 18:35

1 Answer 1

4

The problem is that from is a keyword used by linq for iterating the items of the collection (from item in collection ...).

To solve it use @from in the linq:

var result = from range in inputRanges
             select Enumerable.Range(range.@from, range.to - range.@from + 1);

For more about the @ see: C# prefixing parameter names with @ . IMO the use of the property name from in this case is ok, but I do think in general this would be a bad practice.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh boy...hahaha, thanks. I would have looked for another year without realizing it :D
@D.R. - :) glad it helped you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.