How do I convert DateTime or int to string inside IQueryable? I need to do something like this, but without ToString()
, which throws System.NotSupportedException
exception:
IQueryable filteredEntities = myIQueryableCollection
.Where(o => o.SomeDateTime.ToString().Contains(searchParameter)
|| o.SomeInt.ToString().Contains(searchParameter));
For double and decimal I use SqlFunctions.StringConvert
functions, but there are no overloads for int or DateTime.
Please note that I absolutely need deferred execution there. Calling ToList()
is not an option with the amount of data that I am working with.
Use SqlFunctions.DateName:
data.Where(o => SqlFunctions.DateName("year", o.SomeDateTime).Contains(searchParameter) ||
SqlFunctions.DateName("month", o.SomeDateTime).Contains(searchParameter) ||
SqlFunctions.DateName("weekday", o.SomeDateTime).Contains(searchParameter))
There may be a need to search for other data other than 'year' / 'month' / 'weekday', depending on what would searchParameter
look like.
See MSDN.