سؤال

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.

نصائح أخرى

MSDN seems to suggest using the following extension method:

Enumerable.Cast<TResult>

http://msdn.microsoft.com/en-us/library/bb341406(v=vs.100).ASPX

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top