Linq多条件查询

简介:

 

方法一:

        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;
        }

 

 

方法二:利用linq执行sql,可以使用sql强大的拼接功能

           

 

Default.aspx.cs方法

  ClassLibrary1.Help h = new ClassLibrary1.Help();
            string sqlWhere = "1=1";
            if (txttitle.Text != "")
            {
                sqlWhere += " and title like '%" + txttitle.Text + "%'";
            }
            if (txtlmmc.Text != "")
            {
                sqlWhere += " and lmmc = '" + txtlmmc.Text + "'";
            }

            List<ClassLibrary1.V_Web_News> list = h.getFun(sqlWhere).ToList(); 

 

 

DAL 

public IEnumerable<V_Web_News> getFun(string Where)
{
            string sqlQuery = "select newid,title,lmmc,inputdatetime from V_Web_News";
            if (Where != "")
            {
                sqlQuery += " where " + Where;
            }
            return dc.ExecuteQuery<V_Web_News>(sqlQuery);
}

 

参考博客:http://wenwen.soso.com/z/q272109241.htm

 

目录
相关文章
|
.NET 开发框架 存储
|
机器学习/深度学习 .NET
|
.NET 开发框架
|
SQL .NET 开发框架
|
.NET 索引 开发框架