【发布时间】:2015-03-21 05:48:41
【问题描述】:
我使用 C#,我有以下 LINQ 语句
var WeeklySalesQuery = from d in db.DashboardFigures
join s in outlets.Split(',').Select(x => int.Parse(x)) on d.OutletNo equals s
where (d.TypeOfinformation == "SALES-TW")
group d by new
{
d.TypeOfinformation,
d.Date
} into newGroupedresult
select new Weeklysales()
{
dt = ((DateTime)newGroupedresult.Key.Date),
Sv = (double)newGroupedresult.Sum(d => d.Value_1) + (double)newGroupedresult.Sum(d => d.Value_2),
KPI = 1
};
但是出来的dt格式是
"\/Date(1421884800000+0000)\/
我已尝试将代码更改为
dt = ((DateTime)newGroupedresult.Key.Date).ToString("dd.mm.yyyy")
我收到构建错误,告诉我无法将字符串转换为 DateTime 格式。
【问题讨论】:
-
你有 ms 格式的 json 日期时间。看这里stackoverflow.com/questions/4969757/…newGroupedresult.Key.Date的类型是什么?
-
日期没有格式。该错误意味着您的数据库列包含字符串,而不是日期。 string's 格式表明它是从使用旧 DataContractJsonSerializer 的 Json 字符串插入的。 JSon 没有日期类型,但现在的惯例是使用 ISO 8601 格式
-
如果
Key.Date是一个字符串,你必须像DateTime.Parse(newGroupedresult.Key.Date)那样做某事而不是强制转换。 -
@GiorgosBetsos 这行不通,也不是解决方案。 DateTime.Parse 或 ParseExact 无法识别旧的 Json 格式。
-
真正的解决方案是 1) 添加一个新的 datetime 或 datetimeoffset 字段来保存日期 2) 解析旧的 Json 数据并将日期存储在新的字段中 3) 删除旧的字段并重命名新的一到老的名字