在LINQ中使用DataGridViewRowCollection对象 [英] Using DataGridViewRowCollection object in LINQ

查看:161
本文介绍了在LINQ中使用DataGridViewRowCollection对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用扩展方法和lambda表达式在LINQ表达式中使用 DataGridViewRowCollection 。不幸的是,扩展方法适用于 IEnumerable DataGridViewRowCollection 不实现。有趣的是,我可以在这里使用LINQ这样的SQL语法:

  IEnumerable< DataGridViewRow> lRows = from dGvGrid.Rows中的DataGridViewRow行
select row;

这样做后,我可以使用LINQ扩展方法:

  foreach(DataGridViewRow lRow in lRows.Where(row => row.index> 4)){...} $ b $有没有办法可以将我的 DataGridViewRowCollection 转换成一个 IEnumerable<> 不使用那个长的第一个语句?同样的事情适用于 DataGridViewCellCollection  DataGridViewColumnCollection 



ps。我正在使用.net框架3.5

解决方案

是的,请执行以下操作:

  var rows = yourDataGridViewRowCollection 
.Cast< DataGridViewRow>()
.Where(row => row.index> 4);

这使用 Enumerable.Cast 扩展方法:


Cast< TResult>(IEnumerable)方法
使得可以在非泛型上调用标准查询运算符
通过提供必要的
类型信息
集合。例如,
ArrayList 不实现
IEnumerable< T> ,而是通过调用$
ArrayList 对象,标准查询$ b,
Cast< TResult>(IEnumerable) $ b运算符可以用来查询
的顺序。



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;

After doing that, I can use LINQ extension methods:

foreach (DataGridViewRow lRow in lRows.Where(row => row.index > 4)) { ... }

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. I'm using .net framework 3.5

解决方案

Yes, do this:

var rows = yourDataGridViewRowCollection
               .Cast<DataGridViewRow>()
               .Where(row => row.index > 4);

This uses the Enumerable.Cast extension method:

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 implement IEnumerable<T>, but by calling Cast<TResult>(IEnumerable) on the ArrayList object, the standard query operators can then be used to query the sequence.

这篇关于在LINQ中使用DataGridViewRowCollection对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆