Linq多条件查询

1.

 public IEnumerable<M_Student> ReadCollegeAndName(String collnum, String name)
        {
            return from s in dc.M_Student
                   where
                      (!String.IsNullOrEmpty(collnum) ? s.CollegeNum.Equals

                    (collnum) : true) &&
                    (!string.IsNullOrEmpty(name) ? s.Name.Contains

                    (name) : true)
                   select s;
        }

2.利用Linq执行SQL,运用SQL强大的拼接功能

 public string GetSqlString()
        {
            string s = "1=1";
            if (title.Text!="")
            {
                s += " && title like %" + title.Text + "%";
            }
            if (name.Text!="")
            {
                s += " && name like %" + name.Text + "%";
            }
            return s;
        }

        public List<T> GetList()
        {
            var l = from a in _db.Plate
                    where GetSqlString()
                    select a;
            return l.ToList<T>();
        }

3.推荐这个

原文地址:https://www.cnblogs.com/nanxiaoxiang/p/2625499.html