LINQ-除Contains运算符外,查询运算符的LINQ到SQL实现中不能使用局部序列

LINQ-除Contains运算符外,查询运算符的LINQ到SQL实现中不能使用局部序列,linq,linq-to-sql,contains,iqueryable,Linq,Linq To Sql,Contains,Iqueryable,这似乎是我遇到的最普遍的错误——多篇关于它的帖子都是针对不同的问题——这里有一个新的:) 当枚举以下IQueryable时,我得到上述错误: N.B.项目是一个IQueryable,关键字是一个字符串 items = items.Where(p => p.heading.ToLower().Contains(keywords) || p.description.ToLower().Contains(keywords)); 这是令人困惑的

这似乎是我遇到的最普遍的错误——多篇关于它的帖子都是针对不同的问题——这里有一个新的:)

当枚举以下
IQueryable
时,我得到上述错误:

N.B.
项目
是一个
IQueryable
关键字
是一个
字符串

items = items.Where(p => p.heading.ToLower().Contains(keywords) || 
                         p.description.ToLower().Contains(keywords));

这是令人困惑的,因为正如错误所提示的,当您使用
包含时,它应该可以正常工作-有人知道如何解决此问题吗?

如果关键字是支持枚举的集合,则应该是另一种方式:

items = items.Where(p => keywords.Contains(p.heading.ToLower()) || 
                         keywords.Contains(p.description.ToLower()));

如果items是IQueryable,那么错误可能就在那里,与where语句无关。 在添加where语句之前,是否可以尝试强制枚举

例如,假设您试图将内存中的列表与datatable连接起来,则在计算或枚举查询时会出现该错误

List<tblCategory> categories = tblCategory.ToList();

IQueryable<tblItem> items = (from r in tblItem 
            join c in categories on r.CategoryID equals c.Id select r);


//    items = items.Where(p => p.heading.ToLower().Contains(keywords) || 
//                             p.description.ToLower().Contains(keywords));



var firstMatch = items.FirstOrDefault();

// The error will be generated here even if the where is remmed out 
List categories=tblCategory.ToList();
IQueryable items=(从r到tblItem)
在r上的类别中加入c。类别Id等于c。Id选择r);
//items=items.其中(p=>p.heading.ToLower().包含(关键字)|
//p.description.ToLower()包含(关键字));
var firstMatch=items.FirstOrDefault();
//即使删除了where,也会在此处生成错误

已解决

感谢sgmoore提供的意见-它帮助达成了以下解决方案:

IQueryable
列表关联到
IEnumerable
列表,在其上运行
ToList
,然后在列表上使用我的过滤器,效果非常好

IEnumerable<tblItems> temp = items.ToList();

temp = temp.Where(p => p.heading.ToLower().Contains(keywords) || 
                     p.description.ToLower().Contains(keywords));

items = temp.AsQueryable();
IEnumerable temp=items.ToList();
temp=temp.Where(p=>p.heading.ToLower()。包含(关键字)|
p、 description.ToLower().包含(关键字));
items=临时AsQueryable();

如果关键字是一个集合@Jimbo的代码甚至不会编译。是的,你是对的。获取关键字变量的类型会很有帮助。很抱歉,
keywords
是一个
string
那么您提供的代码是正确的,并且与
items=items相等。其中(p=>p.heading.ToLower()==keywords | | p.description.ToLower()==keywords)
@Eugene:Not true-
Contains
将测试关键字是否是字符串的子字符串-请参见
中的
语句。我刚刚尝试过使用LinqPad和NorthWind DB进行类似的查询。正如@sgmoore所说,问题可能出在
项中
IQueryable
。只要您对将tblItems表中的所有内容都放入内存并执行内存过滤没有问题。