Linq linq-to-entities add join if condition is met -


i quite new c# , ef, , not find answer following question, forgive me should have overlooked one.

i want join "main query" query1 several "optional querys" go different tables in same model, how supposed achieve that?

if try use join() method, works long this:

var query = query1.join(query2 [...]); 

but when trying like

query = query1;  if (condition_is_met) {     query = query.join(query2 [...]); } 

i receive error

cannot implicitly convert type 'system.linq.iqueryable<anonymoustype#1>' 'system.collections.generic.ienumerable<someentity>'. explicit conversion exists (are missing cast?)

how can avoid error, and/or there way add joins if condition met?

thanks in advance , sorry (i fear dumb) question.

you can conditionally add query, need understand once query typed, cannot change type else. problem 1 of queries types anonymous type, , types entity, , inconsistent.

for example works, see:

var query = context.foos.asqueryable();  if (datetime.now.second % 2 == 0) {     query = query.join(context.bars, f => f.barid, b => b.id, (f, b) => new { f, b }).select(item => item.f); } else  {     query = query.orderbydescending(f => f.id); } 

the reason works initial declaration iqueryable<foo>, , each of resulting queries (either join or ordered version) still iqueryable<foo>.

if need project query result anonymous type, recommend saving later step (a query of constructed query). construct of joins, filtering, orders, etc., pull out anonymous type. long type same regardless of other logic including, able build upon single query.

however, if logic affects shape of data retrieve, that's you're out of luck, need go direction.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -