针对多个列搜索单个值

时间:2014-05-12 10:33:27

标签: sql asp.net-mvc entity-framework linq-to-entities

我想从用户发送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");
        }
    }

1 个答案:

答案 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");
    }
}