gpt4 book ai didi

c# - LINQ to DataGridViewRowCollection

转载 作者:太空狗 更新时间:2023-10-30 00:55:51 33 4
gpt4 key购买 nike

关于 DataGridViewRowCollection 上的 LINQ 查询,我有点不解。这是我的查询(其中“网格”是一个 DataGridView 对象):

var rows = from DataGridViewRow row in grid.Rows
where row.Selected
select row;

我有一个包含此查询的项目,它执行得很好。问题是在另一个项目中,当我尝试构建解决方案时出现以下错误:

error CS1936: Could not find an implementation of the query pattern for source type 'System.Windows.Forms.DataGridViewRow'.  'Where' not found.

起初我以为是引用问题,但我在两个项目中使用了相同的引用:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Data.EntityModel;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Diagnostics;

有没有人知道为什么我的 LINQ 查询在一个项目中有效但在另一个项目中无效?

编辑 1:

作为记录,这里是查询正在工作的确切上下文:

public List<Int64> ComponentIDs
{
get
{
return
(
from DataGridViewRow row in grid.Rows
where row.Selected
select (Int64)row.Cells[0].Value

).ToList();

}

}

编辑2:

我刚刚遇到以下 link ...查看已接受的答案...这就是我想要做的。出于某种原因,我无法使用 IEnumerable.Cast() 扩展方法...我错过了什么?

最佳答案

实际上,我不认为您的代码看起来完全像那样。自 DataGridViewRowCollection不执行 IEnumerable<DataGridViewRow>你必须使用 Cast<DataGridViewRow>()像这样:

var rows = from row in grid.Rows.Cast<DataGridViewRow>()
where row.Selected
select row;

关于c# - LINQ to DataGridViewRowCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9195256/

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