score:7

Accepted answer

you can only use the core extension methods and clr methods defined for your ef provider when using entity framework and queries on iqueryable<t>. this is because the query is translated directly to sql code and run on the server.

you can stream the entire collection (using .toenumerable()) then query this locally, or convert this to a method that is translatable directly to sql by your provider.

that being said, basic bitwise operations are supported:

the bitwise and, or, not, and xor operators are also mapped to canonical functions when the operand is a numeric type.

so, if you rewrite this to not use a method, and just do the bitwise operation on the value directly, it should work as needed. try something like the following:

public list<album> getvisiblealbums(int accesslevel)
{
    return rep.find<album>(a => (a.accesslevel & accesslevel > 0)).tolist();  
}

(i'm not sure exactly how your current extension method works - the above would check to see if any of the flags come back true, which seems to match your statement...)

score:2

there are ways to change the linq query just before ef translates it to sql, at that moment you'd have to translate your ''foreign'' method into a construct translatable by ef.

see an previous question of mine how to wrap entity framework to intercept the linq expression just before execution? and mine efwrappablefields extension which does just this for wrapped fields.

score:10

i realize this already has an accepted answer, i just thought i'd post this in case someone wanted to try writing a linq expression interceptor.

so... here is what i did to make translatable custom extension methods: code sample

i don't believe this to be a finished solution, but it should hopefully provide a good starting point for anyone brave enough to see it through to completion.


Related Query

More Query from same tag