vineel
vineel

Reputation: 3683

LINQ should return no records if where contains empty string

I am trying to fetch the car records where car description filter text. But I should not return any records if the filter text is empty. The following code is working but is there a better way to do this avoiding cars.where(c => false)

public JsonResult GetCars(string cFilter)
{
    IQueryable<Cars> cars = CarService.GetCars();
    if (!string.IsNullOrEmpty(cFilter))
        cars = cars.Where(c => c.carDescr.Contains(cFilter));
    else
        cars = cars.Where(c => false);
    var carInfo = (from c in cars
                      select (new { id = c.CarID, description = c.CarID + "-" + c.carDescr })).ToList();
    return Json(carInfo, JsonRequestBehavior.AllowGet);
}

Upvotes: 0

Views: 271

Answers (3)

vineel
vineel

Reputation: 3683

Got another solution. Added cFilter empty or null check in the where clause and it worked.

cars = cars.Where(c => c.carDescr.Contains(cFilter) && !string.IsNullOrEmpty(cFilter));

Complete function below.

public JsonResult GetCars(string cFilter)
        {
            IQueryable<Cars> cars = CarService.GetCars();
            cars = cars.Where(c => c.carDescr.Contains(cFilter) && !string.IsNullOrEmpty(cFilter));
            var carInfo = (from c in cars
                           select (new { id = c.CarID, description = c.CarID + "-" + c.carDescr })).ToList();
            return Json(carInfo, JsonRequestBehavior.AllowGet);
        }

Upvotes: 0

Alper Tunga Arslan
Alper Tunga Arslan

Reputation: 579

I dont know i missed something but you set null only for cFilter is null

public JsonResult GetCars(string cFilter)
{

    IQueryable<Cars> cars = CarService.GetCars();
    if (!string.IsNullOrEmpty(cFilter))
     {cars = cars.Where(c => c.carDescr.Contains(cFilter));
       var carInfo = (from c in cars
                     select (new { id = c.CarID, description = c.CarID + "-" + c.carDescr })).ToList();
      return Json(carInfo, JsonRequestBehavior.AllowGet);
     }   
    else
       {

         return Json("", JsonRequestBehavior.AllowGet);
       } 


}

Upvotes: 0

Jon Hanna
Jon Hanna

Reputation: 113302

You could skip out the use of CarService entirely.

public JsonResult GetCars(string cFilter)
{
  IQueryable<Cars> cars;
  if (!string.IsNullOrEmpty(cFilter))
    cars = CarService.GetCars().Where(c => c.carDescr.Contains(cFilter));
  else
    cars = Enumerable.Empty<Cars>().AsQueryable();

  var carInfo = (from c in cars
                 select (new { id = c.CarID, description = c.CarID + "-" + c.carDescr })).ToList();
  return Json(carInfo, JsonRequestBehavior.AllowGet);
}

You could cache the EnumerableQuery<Cars> produced by Enumreable.Empty<Cars>() or indeed just return a JsonResult for an empty array in any case, since any empty json array is an empty json array:

public JsonResult GetCars(string cFilter)
{
  if (!string.IsNullOrEmpty(cFilter))
    return Json(Enumerable.Empty<object>(), JsonRequestBehavior.AllowGet);

    var carInfo = (from c in CarService.GetCars()
                      where c.carDescr.Contains(cFilter)
                      select (new { id = c.CarID, description = c.CarID + "-" + c.carDescr })).ToList();
    return Json(carInfo, JsonRequestBehavior.AllowGet);
}

I'd go for this latter, but it's worth mentioning the former for cases where it is the only of the two that is applicable.

Upvotes: 2

Related Questions