我想DataGridViewRowCollection在LINQ表达式中使用扩展方法和lambda表达式.不幸的是,扩展方法是类型IEnumerable<T>,它DataGridViewRowCollection没有实现.有趣的是,我可以在这里使用类似SQL的语法LINQ:
IEnumerable<DataGridViewRow> lRows = from DataGridViewRow row in dgvGrid.Rows
select row;
Run Code Online (Sandbox Code Playgroud)
在这之后,我可以使用LINQ扩展方法:
foreach (DataGridViewRow lRow in lRows.Where(row => row.index > 4)) { ... }
Run Code Online (Sandbox Code Playgroud)
有没有什么方法可以将我转换DataGridViewRowCollection成一个IEnumerable<>没有使用那么长的第一个声明?同样的事情适用于DataGridViewCellCollection和DataGridViewColumnCollection.
PS.我正在使用.net framework 3.5
这不起作用.
var result =
from row in grid.Rows
where (string) row.Cells["COLUMN_1"].Value == "Test"
select row.Cells["COLUMN_2"].Value as int?;
Run Code Online (Sandbox Code Playgroud)
但这样做.
var result =
from row in grid.Rows.Cast<DataGridViewRow>()
where (string) row.Cells["COLUMN_1"].Value == "Test"
select row.Cells["COLUMN_2"].Value as int?;
Run Code Online (Sandbox Code Playgroud)
据我所知,DataGridViewRowCollection是DataGridViewRows的集合.那么为什么我必须先将每个成员转换为DataGridViewRow才能访问其属性?
它与DataGridViewRowCollection实现非泛型IEnumerable但非泛型版本的事实有什么关系IEnumerable<DataGridViewRow>吗?或者它显然在ArrayList内部使用?它并没有给我带来任何实际问题,但我想更好地理解它.