Can't Translate Extension Method Into Store Expression

喜你入骨 提交于 2019-12-23 05:13:36

问题


I have an extension method as follows:

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

And i'm trying to use it like this:

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

But i get the error:

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.

If i change the query to:

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

Which is identical to the method.

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

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).

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.

Any ideas?


回答1:


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




回答2:


Change it to:

public static IQueryable<Post> SatisfiesSomeCondition(this IQueryable<Post> query, SomeObj someObj)
{
   int id = someObj.SomeObjId;
   return query.Where(post => post.SomeObjId == id);
}

and use it like:

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

This way it should work. You can combine multiple Where conditions in single query so it should offer you at least basic reusablity.



来源:https://stackoverflow.com/questions/9075021/cant-translate-extension-method-into-store-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!