c# - filtering results with Entity Framework -


another kinda newbie question guess. have ef setup , want select records based on filter. have someclass 4 items (all strings keep things simple, lets call them string1, string2, , on). now, in post send filter in instance of someclass, maybe not properties filled in. might end string1="something", string2="bla" , string4="bla2". string 3 = null. now, how setup query? if try like:

var dataset = entities.mydatabase     .where(x => x.string1 == someclass.string1 && x.string2 == someclass.string2 && x.string3 == someclass.string3 && x.string4 == someclass.string4) .select(x => new { x.string1, x.string2, x.string3, x.string4}).tolist(); 

... no results, because string3=null. checking parameters , see if they're set , create query based on that, there must more elegant that.

anyone?

thanks! ronald

the following return rows someclass.string null or equals x.string.

var dataset = entities.mydatabase     .where(x => someclass.string1 == null || x.string1 == someclass.string1)     .where(x => someclass.string2 == null || x.string2 == someclass.string2)     .where(x => someclass.string3 == null || x.string3 == someclass.string3)     .where(x => someclass.string4 == null || x.string4 == someclass.string4) .select(x => new { x.string1, x.string2, x.string3, x.string4}).tolist(); 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -