I also had to deal with dynamic conditions for doing a DB search. Instead of string parsing or dynamic LINQ, I came up with this solution. errorsOnly, startDate and endDate can (but must not) be set in the frontend. Additional conditions can simply be added accordingly:
var query = from x in db.DoubleDataValueArchive select x;
query = query.Where(x => x.DataPointId != null);
// Check if only errors should be shown (that are listed in errorDps)
List<int> errorDps = new List<int>();
if (errorsOnly.HasValue) {
if (errorsOnly == true)
{
errorDps = db.DataPoints.Where(x => x.DataType == 4).Select(x => x.Id).ToList();
query = query.Where(x => errorDps.Contains((int)x.DataPointId));
}
}
// Start Date
if (startDate.HasValue) {
startDate = startDate.Value.ToUniversalTime();
query = query.Where(x => x.DateValue >= startDate);
}
// End Date
if (endDate.HasValue)
{
endDate = endDate.Value.ToUniversalTime();
query = query.Where(x => x.DateValue <= endDate);
}
...and so on. This is completely dynamic but safe to work with at the same time. The assembled SQL query only gets finally executed once, when you make a list or similar out of the IQueryable.