Vra

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");
        }
    }
Was dit nuttig?

Oplossing

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");
    }
}
Gelisensieer onder: CC-BY-SA met toeskrywing
Nie verbonde aan StackOverflow
scroll top