Linq拼装where查询条件

需要对list多条件判断查询

public List<user> Test(int id, string name)
{
    List<user> list = new List<user>()
    {
      new user() { id = 1, name = "张三" };
      new user() { id = 2, name = "李四" };
    };

    Func<user, bool> exp = x =>
    {
        var id_exp = id > 0 ? x.id = id : true;
        var name_exp = !string.IsNullOrWhiteSpace(name) ? x.name.Contains(name) : true;
        return id_exp && name_exp;
    };
    list = list.where(exp).ToList();
    return list;
}

user实体

public class user
{
  public int id { get; set; }
  public string name { get; set; }
}
posted @ 2021-11-04 15:30  七晨~  阅读(303)  评论(0)    收藏  举报