문제

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