How do you check if an object with a LINQ query is empty?
If the object is empty, I want to omit it from the search.
I do not want to make an If Else statement and duplicate the code and check if the object is Null before the request. At the moment, my request will return an error if the object is not empty. "The Nullable object must have a value."
public ActionResult Search(List<int> accountStatus = null , string accountName = "", int pageId = 1)
{
var model = Db.Entities
.Where(i => i.GroupId == null && i.IsActive)
.Where(an => string.IsNullOrEmpty(accountName) || (an.Name.StartsWith(accountName) || an.Name.Contains(accountName)))
.ToList()
.Where(accs => accountStatus == null || accountStatus.Contains((int)accs.CurrentStatusId.Value))
.OrderByDescending(x => x.CreatedDate);
if (Request.IsAjaxRequest())
{
return PartialView("********", model.ToPagedList(pageId, nbItemsPerPage));
}
return View(model);
}
user4884237
source
share