Entity Framework 5 is what I'm using, and when count() is called from below
string sqlStr = @"SELECT top 20 ID, CAST(DOI as DATETIME) as IDate FROM DTable"; var results = db.Database.SqlQuery<SRVM>(sqlStr); //get total count var total = results.Count();
I experience a bug:
The specified cast from a materialized 'System.DateTime' type to the 'System.String' type is not valid.
Do you know why?
In a linq query, it appears that you are assigning a DateTime to a String variable. Additionally, I'm assuming that the "results.Count()" function, which executes the query and attempts to populate the target object, is the initial part of the query. The identical problem will appear if you switch the Count() call to any function that would carry out the call (ToList(), First(), etc.).
Make sure you call ToString() with a format provider when converting a DateTime to a string match in order to do the conversion.