c# - Often used LINQ returned from method -


i have 1 linq used much. try create method return linq like:

    public static system.linq.expressions.expression<func<myentity, bool>> getfilteredentity() {         return x => true/*some condition*/;     }      public static func<myentity, bool> getfilteredentity() {         return x => true/*some condition*/;     } 

and use

    db.myentities.where(getfilteredentity()); 

is successfull, but! need use like

    db.parententities.where(entity => entity.myentities.where(getfilteredentity())); 

this code compiled too, every time when use it, got error:

system.invalidoperationexception: there open datareader associated command must closed first. 

,even:

db.parententities.where(entity => entity.myentities.where(getfilteredentity())).tolist(); 

throw exception too.

but,

db.parententities.where(entity => entity.myentities.where(x => true/*some condition*/)) 

still works fine! why happend, , have way round this?

final working code

public static expression<func<myentity, bool>> getfilteredentity() { return x => true/*some condition*/; }

and

var expression = getfilteredentity();

db.parententities.where(entity => entity.myentities.asqueryable().where(expression ));

also .asqueryable() passing func parameter in linq entities , 'internal .net framework data provider error 1025' error

in first example function called , translated expression before sent query provider. in next 2 examples function call embedded within expression sent query provider, , query provider doesn't know function call, throws exception. when embed actual expression in expression, there no function call confuse query provider.

as solution, pull out function call variable. query provider is smart enough see used closed on variable, , pull out value. function call it's not sure if should evaluate or try translate should done on db's end. trying of both confusing , hard work with, both query provider, , people using it. simplify matters, function calls expressions never executed prior sending query. closed on variable, there no other way treated, there isn't other behavior confuse with.

var expression = getfilteredentity(); db.parententities.where(entity => entity.myentities.where(expression )); 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -