在 LINQ 中使用 DataGridViewRowCollection 对象 [英] Using DataGridViewRowCollection object in LINQ
问题描述
我想在使用扩展方法和 lambda 表达式的 LINQ 表达式中使用 DataGridViewRowCollection
.不幸的是,扩展方法适用于 IEnumerable
类型,DataGridViewRowCollection
没有实现.有趣的是,我可以在这里使用 LINQ 和类似 SQL 的语法:
I'd like to use a DataGridViewRowCollection
in a LINQ expression using extension methods and lambda expressions. Unfortunately, the extension methods are for types IEnumerable<T>
, which DataGridViewRowCollection
doesn't implement. The funny thing is, I can use LINQ here with the SQL-like syntax:
IEnumerable<DataGridViewRow> lRows = from DataGridViewRow row in dgvGrid.Rows
select row;
这样做之后,我可以使用 LINQ 扩展方法:
After doing that, I can use LINQ extension methods:
foreach (DataGridViewRow lRow in lRows.Where(row => row.index > 4)) { ... }
有什么方法可以将我的 DataGridViewRowCollection
转换为 IEnumerable<>
而不使用第一个长语句?同样的事情适用于 DataGridViewCellCollection
和 DataGridViewColumnCollection
.
Is there any way I can convert my DataGridViewRowCollection
to an IEnumerable<>
without using that long first statement? The same thing applies to DataGridViewCellCollection
and DataGridViewColumnCollection
.
ps.我正在使用 .net 框架 3.5
ps. I'm using .net framework 3.5
推荐答案
是的,这样做:
var rows = yourDataGridViewRowCollection
.Cast<DataGridViewRow>()
.Where(row => row.index > 4);
这使用 Enumerable.Cast
扩展方法:
This uses the Enumerable.Cast
extension method:
Cast
方法启用标准查询运算符在非泛型上调用通过提供必要的集合类型信息.例如,ArrayList
没有实现IEnumerable
,但通过调用Cast
ArrayList
对象,标准查询然后可以使用运算符来查询顺序.
The
Cast<TResult>(IEnumerable)
method enables the standard query operators to be invoked on non-generic collections by supplying the necessary type information. For example,ArrayList
does not implementIEnumerable<T>
, but by callingCast<TResult>(IEnumerable)
on theArrayList
object, the standard query operators can then be used to query the sequence.
这篇关于在 LINQ 中使用 DataGridViewRowCollection 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!