asp.net - How to make a flattening function for linq object (Linq To Entities for mysql)? -


i upgrading old program , using linq basic select, can learn linq in process. have repetitive task of showing data various join grid view, below sample

protected void page_load(object sender, eventargs e) {     using ( vavestockmodel.vavestockentities db = new vavestockmodel.vavestockentities())     {         var prod = (from p in db.products select p);         var prodd = (from p in db.productdetails select p);         var prode = (from p in db.product_extra_data select p);         var join1 = (from p in prod                      join                          pd in prodd                          on p.prstyle equals pd.stylecode                      select new {pr=p,prd=pd }).tolist();         var join2 = (from p in prod                      join                          pd in prodd                          on p.prstyle equals pd.stylecode                      select new { p.prshow,p.prprice,pd.i_ean_51,pd.i_ean_50 }).tolist();         var join3 = (from p in prod                      join                          pd in prodd                          on p.prstyle equals pd.stylecode                      select new { flattenmodel(p),flattenmodel(pd) }).tolist();          response.write(join1);         gridview1.datasource = join1;         gridview1.databind();     }  }  ??object flattenmodel(vavestockmodel.vavestockentities en)//? here want pass table row {     string[] toren;     //foreach (var item in en.)     //{      //}     return toren; } 

join2 can bound gridview while join1 cant because return entity object. since have select columns writing names repetitively many tables not wise choice hence want write function returns flattened data.

but finding difficult proceed. difficulty coming because don't know should returned , passed parameters in case. can 1 point me in right direction proceed?

you can properties of object through system.reflection here example how can values properties , store them in list , can assign list datasource.

entities ent = new entities(); var data = (from x in ent.employees x.id == 1 select x).firstordefault(); list<string> list = converttolist(data);  public list<string> converttolist(object obj)     {         list<string> list = new list<string>();         propertyinfo[] pinfo = obj.gettype().getproperties();         foreach (var info in pinfo)         {             if (info.getvalue(obj) != null)                 list.add(info.getvalue(obj, null).tostring());         }         return list;     } 

edit

you can return datatablefrom anonymous object.

public datatable converttodatatable(ienumerable<dynamic> obj)     {         datatable dt = new datatable();         foreach (dynamic item in obj)         {             datarow dr = dt.newrow();             propertyinfo[] pinfo = item.gettype().getproperties();             foreach (var info in pinfo)             {                 if (!dt.columns.contains(info.name))                     dt.columns.add(info.name, info.propertytype);                  var val= info.getvalue(item, null);                 dr[info.name] = val;              }             dt.rows.add(dr);         }         return dt;     } 

here how call it

entities ent = new entities(); var data = (from x in ent.employees x.id == 1 select x).firstordefault(); datatable dt=converttodatatable(data); 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -