ChrisJJ
ChrisJJ

Reputation: 2292

Why does this simple LINQ query syntax statement not translate to method syntax?

Here's the query syntax version:

DataGridViewRowCollection mydgvrs = new DataGridView().Rows;

IEnumerable<DataGridViewRow> a =
 from DataGridViewRow row in mydgvrs
 where row.Height > 0
 select row;

which is fine, and the method syntax version:

IEnumerable<DataGridViewRow> a2 =
 mydgvrs.Where(row => row.Height > 0);

which the complier rejects - "no extension method Where ... could be found"?

What's up?

Upvotes: 0

Views: 125

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564333

Because you have the type specified in the query syntax version. (The DataGridViewRow in from DataGridViewRow row).

To translate this, you'll need a Cast<T>:

IEnumerable<DataGridViewRow> a2 = mydgvrs.Cast<DataGridViewRow>()
                                         .Where(row => row.Height > 0);

This is required since mydgvrs implements IEnumerable, but not IEnumerable<DataGridViewRow>.

Upvotes: 1

Related Questions