Skip to main content
 首页 » 编程设计

c#之无法在 IEnumerableT> 上找到查询模式的实现

2024年02月20日9softidea

我有一个 IEnumerable<T>作为方法的参数,其中 T是一个结构类型:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using ... 
 
public static class Foo 
{ 
    internal struct CellDiff 
    { 
        public int RefX; 
        public int RefY; 
        public object OldValue; 
        public object NewValue; 
    } 
 
    private static void ItemChanged(IEnumerable<CellDiff> diffs, int cellX, int cellY) 
    { 
        var change = from CellDiff diff in diffs 
                     where diff.RefX == cellX && diff.RefY == cellY 
                     select diff; 
 
        ... 
    } 
} 

这会导致以下错误:

(parameter) IEnumerable<CellDiff> diffs

Error:
Could not find an implementation of the query pattern for source type 'CellDiff'. 'Where' not found.

我也试过 diffs.AsQueryable() , 无济于事。

我在 IEnumerable<T> 上执行 LINQ 查询通常没有问题.我对这里发生的事情有点迷茫。

请您参考如下方法:

在 LINQ 查询语法中指定类型会创建对带有该类型参数的 Cast 扩展方法的调用。

您是否在某处定义了自己的Cast?