I try to filter date in variable IQueryable<MyType> itemsFiltered
from DB:
itemsFiltered = itemsFiltered.Where(i => i.Dataout.Value.ToString("dd.MM.yyyy").Contains(date));
But I get an error The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync()
I can rewrite expression like this:
itemsFiltered = itemsFiltered.Where(i => (
(
i.Dataout.Value.Day.ToString().Length > 1 ?
i.Dataout.Value.Day.ToString() :
"0" + i.Dataout.Value.Day.ToString()
) + "." + (
i.Dataout.Value.Month.ToString().Length > 1 ?
i.Dataout.Value.Month.ToString() :
"0" + i.Dataout.Value.Month.ToString()
) + "." +
i.Dataout.Value.Year.ToString()).Contains(date));
And it works, but looks awful. How to simplify this?
ADD
The problem is: when the user inputs 11, it is unknown, if it means day, month (November) or year (2011).
date
is string
, it can be "11"
or "11.11"
or "01.01"
or "11.01.201"
and so on.