linq-to-sql - 如何在 linqtosql 中将日期时间转换为字符串?

标签 linq-to-sql

我正在使用 linqtosql 并且在 linq 查询中,我尝试将日期时间类型列转换为像“dd-MM-yy”这样的字符串。
但是,我收到如下错误:

NotSupportedException: Method 'System.String ToString(System.String)' has no supported translation to SQL.



以下是我的 linq 查询:
from ffv in Flo_flowsheet_values
where ffv.Flowsheet_key == 2489
&& ffv.Variable_key == 70010558
&& ffv.Status == 'A'
&& ffv.Column_time >= DateTime.ParseExact("2010-06-13 00:00", "yyyy-MM-dd HH:mm", null)
&& ffv.Column_time <= DateTime.ParseExact("2010-06-13 22:59", "yyyy-MM-dd HH:mm", null)
select new  { 
ColumnTime = ffv.Column_time
,ColumnTimeForXCategory = ffv.Column_time.Value.ToString("dd-MM-yy") ***====> this statement invoke error***
,BTValue = Convert.ToDouble( ffv.Value) }

最佳答案

您可以使用 string.FormatToString()方法来解决你的问题。
喜欢

ColumnTimeForXCategory = string.Format("{0:dd-MM-yy}",ffv.Column_time.Value) 

关于linq-to-sql - 如何在 linqtosql 中将日期时间转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3052515/

相关文章:

.net - 使用嵌套 POCO 进行 LINQ To SQL 操作(插入/更新)所需的帮助

linq-to-sql - LINQ to SQL 在使用 DISTINCT 时不生成 ORDER BY?

c# - 查询未映射的类成员

c# - 如何从 Linq 读取大型 XML RAW 到 SQL?

c# - 如何使用 linqto SQL 启用事务处理我的代码?

c# - 任何人都可以将这 3 个 LINQ to SQL 语句缩减为一个吗?

linq-to-sql - Linq2Sql 多对多关系和插入新对象的问题

visual-studio - 在 DBML 设计器中快速查找(搜索)表?

sql - 如何使用 LINQ2SQL 通过存储库在 DB 上运行集成测试?

linq-to-sql - 是否有理由使用一个 DataContext 实例而不是多个实例?