RT 实际有10多个条件,为了简单本例只写了两个条件。 我已将各条件写成string了,又怎么样写成LINQ语句呢?
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UserSearch(FormCollection collection)
{
User obj = new User();
String querystr = "";
UpdateModel(obj,collection.ToValueProvider()); if (!String.IsNullOrEmpty(obj.UserName))
querystr += " && UserName = " + obj.UserName; if (!String.IsNullOrEmpty(obj.PassWord))
querystr += " && PassWord = " + obj.PassWord;
...... var users = (from s in db.users
where querystr //这里提示不能将string隐式转换成bool
select s);
return View(users);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UserSearch(FormCollection collection)
{
User obj = new User();
String querystr = "";
UpdateModel(obj,collection.ToValueProvider()); if (!String.IsNullOrEmpty(obj.UserName))
querystr += " && UserName = " + obj.UserName; if (!String.IsNullOrEmpty(obj.PassWord))
querystr += " && PassWord = " + obj.PassWord;
...... var users = (from s in db.users
where querystr //这里提示不能将string隐式转换成bool
select s);
return View(users);
}
if (!string.IsNullOrEmpty(Request.QueryString["title"]))//条件1
{
orders = orders .And(p => p.Title.Contains(Request.QueryString["title"]));
}
if (!string.IsNullOrEmpty(Request.QueryString["content"]))//条件2
{
orders = orders .And(p => p.Description.Contains(Request.QueryString["content"]));
}你只要能把参数post过去就行了 然后接收判断呗类似的
var BaseData=db.QTable;
PagedList<QTable> orders=null;
var title=Request.QueryString["title"];
var content=Request.QueryString["content"];
if (!string.IsNullOrEmpty(title)&&string.IsNullOrEmpty(content))
orders = (from c in BaseData where c.Title.Contains(title) select c).ToPagedList(id ?? 1, 5);
else if (!string.IsNullOrEmpty(content)&&string.IsNullOrEmpty(title))
orders = (from c in BaseData where c.Description.Contains(content) select c).ToPagedList(id ?? 1, 5);
else if(!string.IsNullOrEmpty(content)&&!string.IsNullOrEmpty(title))
orders = (from c in BaseData where c.Description.Contains(content) && c.Title.Contains(title) select c).ToPagedList(id ?? 1, 5);
else
orders = BaseData.ToPagedList(id ?? 1, 5);