LINQ lambda expression append OR statement

后端 未结 5 1469
攒了一身酷
攒了一身酷 2020-12-21 03:39

If I want to append a AND statement to my query, I can do:

query = query.Where(obj=>obj.Id == id);

if(name.HasValue)
  query = query.Where(obj=>obj.Na         


        
5条回答
  •  臣服心动
    2020-12-21 04:27

    Simply this if I'm not missing something:

    query.Where(obj=>obj.Id == id || (obj.Name == name && name.HasValue))
    

    You might want to read this question (my question...) and answer for more complicated scenarios:
    How to filter IEnumerable based on an entity input parameter

提交回复
热议问题