Wednesday, 15 April 2015

Entity Framework (5) LINQ methods return entire entity collection THEN apply filter -



Entity Framework (5) LINQ methods return entire entity collection THEN apply filter -

i have code this:

user rvalue = null; var userbyid = new func<user, bool>(x => x.userid.equals(userid, stringcomparison.invariantcultureignorecase)); if (this._context.users.any(userbyid)) { var user = this._context.users .include("nested.associations") .single(userbyid); rvalue = user; } homecoming rvalue;

i started profiling query, , noticed ef not applying func<> sql query, collection in-mem after returning entire .users set.

interestingly, next generates query right clause filtering userid:

user rvalue = null; //var userbyid = new func<user, bool>(x => x.userid.equals(userid, stringcomparison.invariantcultureignorecase)); if (this._context.users.any(x => x.userid.equals(userid, stringcomparison.invariantcultureignorecase))) { var user = this._context.users .include("nested.associations") .single(x => x.userid.equals(userid, stringcomparison.invariantcultureignorecase)); rvalue = user; } homecoming rvalue; why ef not build predicate var generated sql? is there way re-use predicate code can remain clean , readable, , have ef stuff generated sql?

ef cannot interpret compiled il code (which func is). need guess original code reflector does. that's not attractive design selection framework doesn't work way. when inline lambda not func. look is analyzable. search "c# look trees" find out more.

yes: utilize expression<func<user, bool>> variable.

entity-framework

No comments:

Post a Comment