If Else in LINQ

后端 未结 6 1584
谎友^
谎友^ 2020-12-01 04:38

Is it possible to use If Else conditional in a LINQ query?

Something like

from p in db.products
if p.price>0
select new
{
  Owner=from q in db.Use         


        
6条回答
  •  难免孤独
    2020-12-01 04:46

    Answer above is not suitable for complicate Linq expression. All you need is:

    // set up the "main query"
    var test = from p in _db.test select _db.test;
    // if str1 is not null, add a where-condition
    if(str1 != null)
    {
        test = test.Where(p => p.test == str);
    }
    

提交回复
热议问题