不能转换扩展方法进店表达 [英] Can't Translate Extension Method Into Store Expression

查看:91
本文介绍了不能转换扩展方法进店表达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展方法如下:

I have an extension method as follows:

public static bool SatisfiesSomeCondition(this Post post, SomeObj someObj)
{
   return post.SomeObjId == someObj.SomeObjId;
}

和我试图用这样的:

var query = ctx.Posts.Where(p => p.SatisfiesSomeCondition(someObj)).ToList();



但我得到的错误:

But i get the error:

LINQ到实体无法识别方法'布尔SatisfiesSomeCondition(xx.xx.xx.Post,xx.xx.xx.SomeObj)的方法,而这种方法不能被翻译成店的表情。

LINQ to Entities does not recognize the method 'Boolean SatisfiesSomeCondition(xx.xx.xx.Post, xx.xx.xx.SomeObj)' method, and this method cannot be translated into a store expression.

如果我更改查询为:

var query = ctx.Posts.Where(p => p.SomeObjId == someObj.SomeObjId).ToList();



这是相同的方法。

Which is identical to the method.

它工作正常,并执行预期的T-SQL。

It works fine, and executes the expected T-SQL.

为什么我第一次查询的工作?这是一个静态方法,不能把它弄清楚如何创建表达式树? (例如一个WHERE过滤器)。当然,我没有先物化查询? (这意味着记录我不想回来过线,我在这里做分页/排序,所以这不是一个选项)。

Why doesn't my first query work? It's a static method, can't it figure out how to create the expression tree? (e.g a WHERE filter). Surely i don't have to materialize the query first? (which means the records i don't want come back over the wire, and i'm doing paging/ordering here, so that's not an option).

当然,我可以什么工作去(如上述),但该方法 SatisfiesSomeCondition 在整个域中使用的现有方法,我想重新使用该功能,而不是复制它。

Of course, i can just go with what works (e.g the above), but the method SatisfiesSomeCondition is an existing method used across the domain and i want to re-use that functionality, not duplicate it.

任何想法?

推荐答案

的LINQ到实体发动机没有办法知道你的静态方法做什么的方式。结果
LINQ查询只能从表达式目录树转换。

The LINQ to Entities engine has no way of knowing what your static method does.
LINQ queries can only be translated from expression trees.

这篇关于不能转换扩展方法进店表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆