LINQ lambda expression append OR statement

后端 未结 5 1470
攒了一身酷
攒了一身酷 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:43

    I would just build this into a single condition:

    if (name.HasValue)
        query = query.Where(obj=>obj.Id == id && obj.Name == name);
    else
        query = query.Where(obj=>obj.Id == id);
    

提交回复
热议问题