gpt4 book ai didi

c# - LINQ 行索引检索转换要求

转载 作者:太空宇宙 更新时间:2023-11-03 18:37:16 25 4
gpt4 key购买 nike

全部,我在 DataGridView 中有行(DGV)。为了测试添加到此 DGV 的项目是否已经存在,我编写了一个方法,该方法使用以下 LINQ 查询返回匹配行的行索引(而不是 for/foeach 循环):

IEnumerable<int> rowCol = this.dataGridViewAttachList.Rows.Cast<DataGridViewRow>()
.Where(row => row.Cells[(int)DgvColumns.DatabaseName].Value.ToString()
.Equals(databaseName, StringComparison.OrdinalIgnoreCase))
.Select(row => row.Index);

我的问题是:为什么我必须使用 Cast<DataGridViewRow>() 进行转换? ? 我第一次尝试没有它,我花了一段时间才弄明白;但我不清楚为什么有必要这样做?

感谢您的宝贵时间。

最佳答案

是因为DataGridViewRowCollection工具 IEnumerable而不是 IEnumerable<DataGridViewRow> .

关于c# - LINQ 行索引检索转换要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13520295/

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