c# - Accessing a property in a LINQ WHERE statement by name -


just wondering whether possible. have various linq queries against entity framework context, like:

 iqueryable<metadata> metadatalist = context.metadata.asqueryable();  metadatalist = metadatalist.where(md => md.metatagfilter1.equals(keyvaluemetatagfilter.metatagvalue, stringcomparison.invariantcultureignorecase));  metadatalist = metadatalist.where(md => md.metatagfilter2.equals(keyvaluemetatagfilter.metatagvalue, stringcomparison.invariantcultureignorecase));  metadatalist = metadatalist.where(md => md.metatagfilter3.equals(keyvaluemetatagfilter.metatagvalue, stringcomparison.invariantcultureignorecase)); 

as see, identical except property being used: metatagfilter1, metatagfilter2, metatagfilter3.

instead of repeating on , over, write method 'filter' can call like:

metadatalist = metadatalist.filter("metatagfilter1");   metadatalist = metadatalist.filter("metatagfilter2");  metadatalist = metadatalist.filter("metatagfilter3");  

so property name passed string, , property should used in linq query.

i tried passing property func , reflection, linq entities cannot translate statements if used in linq query.

any idea if possible?

use linqkit. allows use methods in query long return expression trees.

so create expression in method based on parameter describes property use.

see http://www.albahari.com/nutshell/linqkit.aspx


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -