Linq to SQL 一次格式化日期时间
Linq to SQL Format Date time in One Take
在 Whosebug 上多次询问如何在 linq 中格式化日期时间到 sql 表达式,解决方案总是首先使用 ToList() 强制编译 linq,然后处理使用 .ToString("hh:mm") 格式化选项的奢侈日期时间。
但是我试图一次性完成并且我部分成功了,除了代码既糟糕又丑陋,任何更短的方法来做到这一点,假设日期对象是一个 unix 时间戳存储在db,我正在尝试 return 只是时间部分 4:54 pm。
production_cycles = from p in db.ProductionCycles
where p.IsRunning == true
select new Rest.ProductionCycle {
id = p.ID,
name = p.Name,
created = p.Created,
steps = from s in p.Logs
where user_permissions.Contains(s.Permission.ID)
orderby s.ID ascending
select new Rest.Step {
created = s.Created,
created_label = DbFunctions.CreateDateTime(
(int)SqlFunctions.DatePart("yyyy", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
(int)SqlFunctions.DatePart("m", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
(int)SqlFunctions.DatePart("d", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
(int)SqlFunctions.DatePart("hh", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
(int)SqlFunctions.DatePart("mi", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
0
).Value.Hour.ToString()
+ ":" +
DbFunctions.CreateDateTime(
(int)SqlFunctions.DatePart("yyyy", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
(int)SqlFunctions.DatePart("m", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
(int)SqlFunctions.DatePart("d", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
(int)SqlFunctions.DatePart("hh", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
(int)SqlFunctions.DatePart("mi", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
0
).Value.Minute.ToString()
,
username = s.User.Name,
请试试这个:
var baseDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
production_cycles = from p in db.ProductionCycles
where p.IsRunning == true
select new Rest.ProductionCycle
{
id = p.ID,
name = p.Name,
created = p.Created,
steps = from s in p.Logs
where user_permissions.Contains(s.Permission.ID)
orderby s.ID ascending
select new Rest.Step
{
created = s.Created,
created_label = DbFunctions.DiffHours(baseDate, s.Created).ToString + ":" + DbFunctions.DiffMinutes(baseDate, s.Created).ToString + ":" +DbFunctions.DiffSeconds(baseDate, s.Created).ToString
////// the rest as usual
},
};
在 Whosebug 上多次询问如何在 linq 中格式化日期时间到 sql 表达式,解决方案总是首先使用 ToList() 强制编译 linq,然后处理使用 .ToString("hh:mm") 格式化选项的奢侈日期时间。
但是我试图一次性完成并且我部分成功了,除了代码既糟糕又丑陋,任何更短的方法来做到这一点,假设日期对象是一个 unix 时间戳存储在db,我正在尝试 return 只是时间部分 4:54 pm。
production_cycles = from p in db.ProductionCycles
where p.IsRunning == true
select new Rest.ProductionCycle {
id = p.ID,
name = p.Name,
created = p.Created,
steps = from s in p.Logs
where user_permissions.Contains(s.Permission.ID)
orderby s.ID ascending
select new Rest.Step {
created = s.Created,
created_label = DbFunctions.CreateDateTime(
(int)SqlFunctions.DatePart("yyyy", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
(int)SqlFunctions.DatePart("m", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
(int)SqlFunctions.DatePart("d", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
(int)SqlFunctions.DatePart("hh", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
(int)SqlFunctions.DatePart("mi", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
0
).Value.Hour.ToString()
+ ":" +
DbFunctions.CreateDateTime(
(int)SqlFunctions.DatePart("yyyy", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
(int)SqlFunctions.DatePart("m", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
(int)SqlFunctions.DatePart("d", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
(int)SqlFunctions.DatePart("hh", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
(int)SqlFunctions.DatePart("mi", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))),
0
).Value.Minute.ToString()
,
username = s.User.Name,
请试试这个:
var baseDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
production_cycles = from p in db.ProductionCycles
where p.IsRunning == true
select new Rest.ProductionCycle
{
id = p.ID,
name = p.Name,
created = p.Created,
steps = from s in p.Logs
where user_permissions.Contains(s.Permission.ID)
orderby s.ID ascending
select new Rest.Step
{
created = s.Created,
created_label = DbFunctions.DiffHours(baseDate, s.Created).ToString + ":" + DbFunctions.DiffMinutes(baseDate, s.Created).ToString + ":" +DbFunctions.DiffSeconds(baseDate, s.Created).ToString
////// the rest as usual
},
};