c# - Linq to Sql- Dynamic column select -


iam working on linq-to-sql trying column name passes on dynamic obj parameter(changes every time). need select column name define dynamic obj array.

p.s: need select sku,vendorname,vendorstylecode @ once

"where" clause condition working fine need select specific column define in data.col index

i tried below code not helping me:

public httpresponsemessage postgeneratefile([frombody] dynamic data) { string[] vendorname = data.vendorname != null ?data.vendorname.toobject<string[]>() : null; string[] brandname = data.brandname != null ? data.brandname.toobject<string[]>() : null;  using (var context = new vendor_invdatacontext())         { var query = context.allinventories.asqueryable();               (int = 0; < data.col.count; i++)             {                 if(data.col[i]=="sku")                    query.select(s => s.sku);                 if (data.col[i] == "vendorname")                     query.select(s => s.vendorname);                 if (data.col[i] == "vendorstylecode")                     query.select(s => s.vendorstylecode);                 if (data.col[i] == "stylecode")                     query.select(s => s.stylecode);                 if (data.col[i] == "stylename")                     query.select(s => s.stylename);             }   if (vendorname != null && vendorname.length > 0)             {                 query = query.where(s => vendorname.contains(s.vendorname));             }             if (brandname != null && brandname.length > 0)             {                 query = query.where(s => brandname.contains(s.brandname));             } var items = query.tolist(); 

you need assign result of query.select(...) query variable change query, in case not doing select don't have effect. once have projection of result , rest of fields won't available neither add more fields projection nor add conditions.

so approach never work select statement. need build dynamic expression tree select desired properties instead. fortunately, has worked on here, please @ @dotlatice answer need imo.

so, code be:

  1. apply condition doing now
  2. build expression tree produces anonymous type containing desired columns

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -