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?

D.R.
  • 20,268
  • 21
  • 102
  • 205
  • If I use `.Item1` and `.Item2` instead it works - are named tuples a problem for LINQ? Why? – D.R. Nov 15 '18 at 18:35

1 Answers1

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.

Gilad Green
  • 36,708
  • 7
  • 61
  • 95