The specified cast from a materialized 'System.DateTime' type to the 'System.String' type is not valid

asp.net c# entity-framework

Question

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?

1
6
9/9/2015 6:29:51 AM

Accepted Answer

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.

5
5/28/2014 2:33:36 PM

Popular Answer

ZZZ_tmp


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