现在的位置: 首页 > 综合 > 正文

linq 多条件组织查询核心代码

2013年09月15日 ⁄ 综合 ⁄ 共 937字 ⁄ 字号 评论关闭

方法一:
        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);
}

抱歉!评论已关闭.