DateTime or int to string inside IQueryable?

c# entity-framework entity-framework-6 iqueryable

Question

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.

1
4
5/5/2014 3:19:32 PM

Accepted Answer

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.

6
5/5/2014 3:29:59 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow