gpt4 book ai didi

c# - 从具体化 'System.DateTime' 类型到 'System.String' 类型的指定转换无效

转载 作者:太空狗 更新时间:2023-10-29 21:17:28 25 4
gpt4 key购买 nike

我正在使用 Entity Framework 5,当从下面调用 count() 时

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

我得到错误:

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

有什么想法吗?

最佳答案

看起来您正在将 DateTime 放入 linq 查询中的 String 变量中。我还猜测查询的第一个是“results.Count()”,它实际上正在执行查询并试图填充您正在填充的任何对象。如果将 Count() 调用更改为将执行调用的任何方法(ToList()、First()、...),您将看到相同的错误。

当您将 DateTime 投影到字符串匹配中时,请确保您使用格式提供程序调用 ToString() 以进行转换。

关于c# - 从具体化 'System.DateTime' 类型到 'System.String' 类型的指定转换无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23914429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com