閱讀620 返回首頁    go 阿裏雲 go 技術社區[雲棲]


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

 

參考博客:https://wenwen.soso.com/z/q272109241.htm

 

最後更新:2017-04-02 22:16:21

  上一篇:go 給Magento的模塊保存添加事務(transaction)
  下一篇:go objective-c麵試題