LINQ 异常:“不支持使用本地集合的查询。”但不使用本地集合
以 AdventureWorks 数据库的 Products 表为例,我创建了一个 DBML 并扩展了 DataContext 的属性以包含一个新属性:
partial class AdventureWorksDataContext { public IQueryable<Product> FinishedProducts { get { return Products.Where(p => p.FinishedGoodsFlag); } } }
Products 属性是生成的 DataContext 的一部分,我所做的只是添加一个Where from the Table< ;产品>所以它返回一个 IQueryable。
现在,当尝试像这样查询它时,问题就出现了(愚蠢的示例,但应该显示问题):
var queryFinishedProducts = datacontext.FinishedProducts.Where(fp => fp.ProductID == datacontext.FinishedProducts. Max(p => p.ProductID));
迭代此查询会导致“不支持使用本地集合的查询”异常。我不明白为什么当没有使用本地集合时它会抛出该错误。如果我针对普通的产品表(即 Table
)运行它:
var queryProducts = datacontext.FinishedProducts.Where(fp => fp.ProductID == datacontext.Products .Max(p => p.ProductID));
...工作正常。唯一的区别是我向 Table
添加了一个Where,并将其作为 IQueryable
返回。
有人有什么想法吗?
Using the AdventureWorks database's Products table as the example, I have created a DBML and extended the properties of the DataContext to include a new property:
partial class AdventureWorksDataContext { public IQueryable<Product> FinishedProducts { get { return Products.Where(p => p.FinishedGoodsFlag); } } }
The Products property is part of the generated DataContext and all I've done is add a Where from the Table<Product> so it returns an IQueryable.
Now, the problem comes in when trying to query it like this (dumb example but one that should show the problem):
var queryFinishedProducts = datacontext.FinishedProducts.Where(fp => fp.ProductID == datacontext.FinishedProducts.Max(p => p.ProductID));
Iterating this query results in "Queries with local collections are not supported" exception. I don't understand why it would throw that error when there are no local collections being used. If I run it against the normal Products table (which is a Table<Product>
):
var queryProducts = datacontext.FinishedProducts.Where(fp => fp.ProductID == datacontext.Products.Max(p => p.ProductID));
...it works fine. The only difference is that I added a Where to the Table<Product>
and returned it as an IQueryable<Product>
.
Anyone have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种可行的方法:
或者假设
ProductID
是唯一的,尝试按如下方式重写您的查询:Here's one approach that should work:
Or assuming that
ProductID
is unique try rewriting your query as follows:我能够重现这种行为。以下是反射器中需要查看的更多类型:
基于此堆栈跟踪。
我想知道为什么
Table
类型的属性的处理方式与IQueryable
甚至ITable
类型的属性不同。属性的实现并不重要,返回类型很重要。IQueryable
类型的属性的处理方式肯定与IQueryable
类型的本地作用域变量不同,query1表现出原始行为(异常、本地序列)。
query2 生成此 sql - 不太理想。
query3 在查询翻译期间过度急切地发出子查询,然后对主查询进行第二次往返。
I was able to reproduce this behavior. Here's a couple more types to look at in reflector:
Based on this stack trace.
I wonder why a property of type
Table<T>
is treated differently than a property of typeIQueryable<T>
or evenITable<T>
. The implementation of the property doesn't matter, the return type matters.A property of type
IQueryable<T>
is definately treated differently than a locally scoped variable of typeIQueryable<T>
query1 exhibits the original behavior (exception, local sequences).
query2 generates this sql - less than ideal.
query3 over-eagerly issues the subquery during query translation, then makes a second roundtrip for the main query.