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.推荐这个

 

 

posted @ 2012-08-06 17:23  南潇湘  阅读(354)  评论(0编辑  收藏  举报