c# - How to query based on properties of a model? -


for sake of brevity, assume have model named participants

public class participant() {    public int? id {get; set;}    public string name {get; set;}    public datetime joindate {get; set;}    public string address1 {get; set;}    public string city {get; set;}    public string county {get; set;} }  public ilist<participant> searchparticipants(participant objparticipant) {    using (db.context())    {         //how can acheive this? using ef         //warning pseudo-code / magic function (searchmatches) below          return db.entities<participant>().searchmatches(objparticipant);    } } 

basically, not want construct multiple .where(k => k.propertyname) queries. think php mvc frameworks have this, pass object properties filled in , 1 gets array (in our case ilist) of matching results db.

you can using combination of reflection, loop through properties, , dynamic linq build predicates.

below simple example starting point / proof of concept.

currently uses null value indicating don't want clause, if had value type property (like int) add attribute [filterignore] or , check when loop through properties.

make new console application , replace program.cs add dynamic linq file sample project:

using system; using system.collections.generic; using system.linq; using system.linq.dynamic;  class myobject {     // properties     public string propertya { get; set; }     public string propertyb { get; set; }     public datetime? propertyc { get; set; }      static void main(string[] args)     {         // our repository         var list = new list<myobject>() {                 new myobject() { propertya = "test"} ,                 new myobject() { propertyb = "test"} ,                 new myobject() { propertyc = datetime.today} ,                 new myobject() { propertyc = datetime.today,                                  propertya = "test"}              };          // loop through filtered results         // (calling our getbyexample method         // new object property set)         foreach (var obj in                   getbyexample(list,                               new myobject() { propertyc = datetime.today }))         {             // write output can see working             console.writeline("found object");             console.writeline(obj.propertya);             console.writeline(obj.propertyb);             console.writeline(obj.propertyc);         }          // wait user press key before close         console.readkey();     }      static iqueryable<myobject> getbyexample(list<myobject> objects,                                              myobject filterobj)     {         // create our query variable we'll add         var filteredobjects = objects.asqueryable();          // loop through each property on object         foreach (var prop in filterobj.gettype().getproperties())         {             // value of property             var propval = prop.getvalue(filterobj, null);              if (propval != null)             {                 // if property isn't null add clause                 filteredobjects =                     filteredobjects.where(string.format("{0} = @0",                                                         prop.name),                                           propval);             }         }          return filteredobjects;     } } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -