c# - Exclude linq join condition based on parameter -


i want able dynamically exclude join based on boolean parameters, @ code below, how can exclude join if 'includejoin' variable false or there way dynamically add joins

 class program {     static void main(string[] args)     {         list<foo> foolist = new list<foo>();         foolist.add(new foo{foo_id = 1});         foolist.add(new foo{foo_id = 2});          list<bar> barlist = new list<bar>();         barlist.add(new bar{foo_id = 1});         barlist.add(new bar{foo_id = 1});          iqueryable<foo> fooquery = foolist.asqueryable();         iqueryable<bar> barquery = barlist.asqueryable();          bool includejoin = false;          var foos = f in foolist                     //exclude join if includejoin vairable false!!                    join b in barlist on f.foo_id equals b.foo_id g                    result in g.defaultifempty()                      select new foo { foo_id = f.foo_id };          var results = foos.tolist();     }      public class foo     {         public int foo_id { get; set; }     }      public class bar     {         public int foo_id { get; set; }     } } 

i think want possible building 2 different linq queries:

bool includejoin = false;  ienumerable<foo> foos;  if (includejoin) {     foos = f in foolist                  //exclude join if includejoin vairable false!!                 join b in barlist on f.foo_id equals b.foo_id g                 result in g.defaultifempty()                   select new foo { foo_id = f.foo_id }; } else {     foos = f in foolist select new foo { foo_id = f.foo_id }; }  var results = foos.tolist(); 

with solution, can build 2 independent linq queries result in ienumerable in either way. both result in same type (ienumerable), can use foos.tolist() @ end list values.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -