DateTime or int for string inside IQueryable?

How to convert DateTime or int to string inside IQueryable? I need to do something like this, but without ToString(), which throws an exception System.NotSupportedException:

IQueryable filteredEntities = myIQueryableCollection
    .Where(o => o.SomeDateTime.ToString().Contains(searchParameter) 
        || o.SomeInt.ToString().Contains(searchParameter));

For double and decimal, I use functions SqlFunctions.StringConvert, but there are no overloads for int or DateTime.

Please note that I absolutely need to delay execution. Calling is ToList()not an option with the amount of data I'm working with.

+4
source share
2 answers

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))

, 'year'/'month'/'weekday', , searchParameter.

. MSDN.

+6
-3

Source: https://habr.com/ru/post/1539302/


All Articles