Why does this simple LINQ query syntax statement not translate to method syntax?
Here's the query syntax version:
DataGridViewRowCollection my开发者_StackOverflow社区dgvrs = 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?
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>
.
精彩评论