我想从用户发送searchString
,并希望针对我的数据库的Order
表的多列搜索它。例如,如果用户发送s字符串" abc"那么我必须在订单表中搜索customerEmail
字段是否包含" abc"或" OrderId"字段包含" abc"或"PostelCode"
字段包含" abc"
然后将返回所有这些累积记录。
public ActionResult SearchOrders(string searchString)
{
var searchedOrders = dbContextObject.Orders.Where( //here comes the actual comparison)
if (searchedOrders.Count > 0)
{
return View("_searchedOrders", searchedOrders); ;
}
else
{
return Json("No Record found");
}
}
答案 0 :(得分:1)
public ActionResult SearchOrders(string searchString)
{
var q = searchString.Trim().ToLower();
var searchedOrders = dbContextObject.Orders.Where( t =>
t.CustomerEmail.ToLower().Contains(q) ||
t.OrderId.ToLower().Contains(q) ||
t.PostelCode.ToLower().Contains(q));
if (searchedOrders.Count > 0)
{
return View("_searchedOrders", searchedOrders);
}
else
{
return Json("No Record found");
}
}