gpt4 book ai didi

c# - 在 LINQ 中使用 DataGridViewRowCollection 对象

转载 作者:IT王子 更新时间:2023-10-29 04:24:15 25 4
gpt4 key购买 nike

我想使用 DataGridViewRowCollection在使用扩展方法和 lambda 表达式的 LINQ 表达式中。不幸的是,扩展方法适用于类型 IEnumerable<T> ,这DataGridViewRowCollection不执行。有趣的是,我可以在这里使用类似 SQL 语法的 LINQ:

IEnumerable<DataGridViewRow> lRows = from DataGridViewRow row in dgvGrid.Rows 
select row;

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

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

有什么方法可以转换我的 DataGridViewRowCollectionIEnumerable<>不使用那么长的第一个语句?同样的事情适用于 DataGridViewCellCollectionDataGridViewColumnCollection .

附言。我正在使用 .net Framework 3.5

最佳答案

是的,这样做:

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

这使用了 Enumerable.Cast 扩展方法:

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.

关于c# - 在 LINQ 中使用 DataGridViewRowCollection 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2648657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com