I want to send a searchString from user and want to search it against mutiple columns of a Order table of my database. E.g if user send s string "abc" then i have to search in order table if customerEmail field contain "abc" or "OrderId" field contain "abc" or "PostelCode" field contains "abc" then all these accumulative records will be returned.

 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");
        }
    }
有帮助吗?

解决方案

Have you tried this?

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");
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top