All, I have rows in a DataGridView (DGV). To test whether the item being added to this DGV is already present I have written a method which uses the following LINQ query to return the row indexes of matching rows (instead of a for/foeach loop):
IEnumerable<int> rowCol = this.dataGridViewAttachList.Rows.Cast<DataGridViewRow>()
.Where(row => row.Cells[(int)DgvColumns.DatabaseName].Value.ToString()
.Equals(databaseName, StringComparison.OrdinalIgnoreCase))
.Select(row => row.Index);
My question is: Why do I have to cast using Cast<DataGridViewRow>()? I had first tried without it and it took me a while to work this out; but it is not clear to me why it is necessary?
Thanks for your time.
It’s because
DataGridViewRowCollectionimplementsIEnumerableand notIEnumerable<DataGridViewRow>.