c# - EF code-first - IQueryable having multiplication on a specific decimal property -


i have been struggling following problem on entity framework code-first.

i have entity class decimal, , have multiplier decimal parameter.

i want build query (but not call it), returns entities, bar property need multiplied parameter.

from coding side:

public class foo {     public guid id { get; set; }     public decimal bar { get; set; } }  // simple stuff returns entities after filterings. context.set<foo>().where(x => querying on many props).tolist(); 

this method similiar want achieve:

public iqueryable<foo> getfooquery( .. many properties used query .. , decimal multiplier) {     var iqueryablepart = context.set<foo>().where(querying parameters);      /* ... , here? ... */     /* iqueryablepart = iqueryablepart.select(x => new {            bar = bar * multiplier <-- okay        }); */     // how retrieve other columns without listing them 1 one, , how return data iqueryable<foo> ?      return iqueryablepart; } 

i use method in following way:

iqueryable<foo> fullquery = null;     for(some loop, may 10 or 1000 iterations, depends) {     var part = getfooquery(/* .. query params ..*/, 2);      if(myfullquery == null)          fullquery = part;     else          fullquery.union(part); }  // , in end, db call once:  var result = fullquery.tolist(); 

in sql, handle this:

select      id,     bar * @myvalue bar,      # , other columns      foo       (param queries 1) or      (param queries 2) or     ---     (param queries n) 

my question is: way via iqueryable , ef? important, need call db 1 time.

i reckon may query building stuff, i'm not familiar yet, appreciated.

ef6 not support projection (select) class mapped entity. hence option have project anonymous or special class. scenario, easiest see class this:

public class foobar {     public foo foo { get; set; }     public decimal bar { get; set; } } 

then single query method this:

public iqueryable<foobar> getfooquery( .. many properties used query .. , decimal multiplier) {     return context.set<foo>()         .where(querying parameters)         .select(foo => new foobar         {             foo = foo,             bar = foo.bar * multiplier         }); } 

now can build full query:

iqueryable<foobar> fullquery = null;  (some loop, may 10 or 1000 iterations, depends) {     var subquery = getfooquery(/* .. query params ..*/, 2);      fullquery = fullquery == null ? subquery : fullquery.union(subquery); } 

note if use different multiplier (otherwise whole procedure not make sense), you'd better use linq concat method (which translates sql union all) rather union (which translates sql union).

finally, can materialize result foo sequennce executing single final sql query, switching linq objects , converting foobar foo this:

var result = fullquery.     .asenumerable() // db query ends here     .select(foobar =>     {         foobar.foo.bar = foobar.bar;         return foobar.foo;     })     .tolist(); 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -