How do I implement a dynamic 'where' clause in LINQ?

后端 未结 7 2114
有刺的猬
有刺的猬 2020-12-29 09:57

I want to have a dynamic where condition.

In the following example:

var opportunites =  from opp in oppDC.Opportunities
                        


        
7条回答
  •  Happy的楠姐
    2020-12-29 10:42

    If you know in advance all possible where queries like in the SQL example you have given you can write the query like this

    from item in Items
    where param == null ? true : ni.Prop == param
    select item;
    

    if you don't know all possible where clauses in advance you can add where dymically for example like this:

    query = query.Where(item => item.ID != param);
    

提交回复
热议问题