9

I was working with a DataTable and noticed that Resharper recommended that I can convert a loop into a LINQ expression. I did so and it was rewritten in query expression syntax (simplified):

var test1 = from DataRow row in dt.Rows select row;

Personally, I prefer method syntax so rewrote it to this:

var test2 = dt.Rows.Select(row => row);

And it broke.

'System.Data.DataRowCollection' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Data.DataRowCollection' could be found (are you missing a using directive or an assembly reference?)

Since query expression are translated to method calls, why is that the first works but not the second? I expected either both or neither to work which is obviously not the case.

Lester
  • 3,973
  • 2
  • 24
  • 28

1 Answers1

12

The first has an explicitly typed range variable, so it's actually compiled to:

var test2 = dt.Rows.Cast<DataRow>();

(There's no need for the Select as this is a degenerate query expression (the select is a no-op.)

An alternative is to call AsEnumerable from DataTableExtensions. I believe there may be some performance benefits in that, but only in some cases:

var test2 = dt.AsEnumerable();
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • 2
    Which is necessary because `DataRowCollection` implements `IEnumerable`, not `IEnumerable`. – Chris Shouts Feb 29 '12 at 16:29
  • 1
    @ChrisShouts: Ah, I'd noticed this before but never quite twigged exactly why. The fact it implements the "wrong" IEnumerable is what I was missing. :) – Chris Feb 29 '12 at 16:36