在LINQ使用DataGridViewRowCollection对象 [英] Using DataGridViewRowCollection object in LINQ
问题描述
我想使用扩展方法和lambda表达式的LINQ表达式中使用 DataGridViewRowCollection
。不幸的是,扩展方法是类型的IEnumerable< T>
,其中 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 Framework 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:
的演员LT; TResult>(IEnumerable的)
方法
使标准查询操作符
上的非通用
的集合被调用通过提供必要的
型信息。例如,
的ArrayList
未实现
的IEnumerable< T>
,但通过调用
演员LT; TResult>(IEnumerable的)
在
的ArrayList
对象,标准查询$ b然后$ b运营商可以用来查询
中的序列
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屋!