I have a few filters, which they affect taking rows of items in consideration. Filters: DateFrom, DateTo, search (FirstName, LastName, Email)
here is the query
query = repository.GetAll("Location", "Service", "Employee")
.Where(x => x.CompanyId == companyId && x.LocationId == locationId && !x.Deleted && x.IsConfirmed) //this Where clause works
.Where (x => ((x.FirstName + " " + x.LastName).Contains(search) && !string.IsNullOrEmpty(x.FirstName)) || (x.EmailAddress).Contains(search) && !string.IsNullOrEmpty(x.EmailAddress) ||
((x.Employee.FirstName).Contains(search) && !string.IsNullOrEmpty(x.Employee.FirstName))) //this caluse is causing the issue
.Where(y => (((dateTimeFilterFrom.HasValue && y.Date >= dateTimeFilterFrom)) || !dateTimeFilterFrom.HasValue) && (((dateTimeFilterTo.HasValue && y.Date <= dateTimeFilterTo)) || !dateTimeFilterTo.HasValue)) //this where clause also works
.OrderByDescending(x => x.Date).ThenByDescending(x => x.Start).Skip(itemsPerPage * (currentPage - 1)).Take(itemsPerPage);
}
List<Schedule> result = query.ToList();
return result;
https://i.imgur.com/KneJhf7.png
I would like to ignore it, if nothing is typed. Now it returns 0 rows, probably because I put wrong && statement somewhere
Cases:
This is .net 4.8
A much easier and quicker way is to have 2 different queries - one for a filtered result and one for a non-filtered result if the user does not type anything into the search field.
This check can be done locally and not in the database, which is cluttering your query.
Something like this should work (this excludes the extra .Where
clauses you have for location/date/... filtering for brevity):
var shouldFilterData = !string.IsNullOrEmpty(search);
if (shouldFilterData)
query = repository
.GetAll("Location", "Service", "Employee")
.Where(x => (x.FirstName + " " + x.LastName + " " + x.EmailAddress).Contains(search));
else
query = repository
.GetAll("Location", "Service", "Employee");
List<Schedule> result = query.ToList();
return result;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.