不能隐式转换'System.DateTime?'& # 39; int ? & # 39;c# Linq

本文关键字:int Linq System 转换 不能 DateTime | 更新日期: 2023-09-27 18:13:06

代码:它突出显示了最底部的部分,我可以尝试告诉Linq查找shipto列并将另一个表上的日期时间戳更新一定数量的分钟,并告诉我不能隐式转换类型'System.DateTime?' to 'int?我不知道该如何继续下去……

  var TestingLinq = (from a in db.shp_master
                     join b in db.shp_status on a.serialnbr equals b.serialnbr into b_join
                     from b in b_join.DefaultIfEmpty()
                     where
                               a.shipto == "1022a" &&
                               a.status == "s" &&
                               b.status == "s" &&
                               a.shpreq == 0
                     group new {a, b} by new {
                               a.serialnbr,
                               a.trailer,
                               a.shipto
                     } into g
                     orderby
                               g.Key.serialnbr
                     select new RecieveTruck {
                               SerialNumber = g.Key.serialnbr,
                               TrailerNumber = (g.Key.trailer ?? "N/A"),
                               Shipped = g.Min(p => p.b.datetimestamp),
                               ETA = 
                               g.Key.shipto == "1026" ? (System.DateTime?)Convert.ToDateTime(g.Min(p => p.b.datetimestamp)).AddMinutes(180) :
                               g.Key.shipto == "2020" ? (System.DateTime?)Convert.ToDateTime(g.Min(p => p.b.datetimestamp)).AddMinutes(195) :
                               g.Key.shipto == "2017" ? (System.DateTime?)Convert.ToDateTime(g.Min(p => p.b.datetimestamp)).AddMinutes(180) : 
                               g.Key.shipto == "nor" ? (System.DateTime?)Convert.ToDateTime(g.Min(p => p.b.datetimestamp)).AddMinutes(180) : null
                     });
  return View(TestingLinq);

给出标题....中的错误我试图将此SQL查询转换为Linq:

SELECT a.serialnbr, ISNULL(a.trailer, 'N/A') AS 'trailer', MIN(b.datetimestamp) AS 'datetimestamp', 
             CASE WHEN a.shipto = '1026' THEN DATEADD(mi, 180, MIN(b.datetimestamp)) 
                  WHEN a.shipto = '2020' THEN DATEADD(mi, 195, MIN(b.datetimestamp)) 
                  WHEN a.shipto = '2017' THEN DATEADD(mi, 180, MIN(b.datetimestamp)) 
                  WHEN a.shipto = 'nor' THEN DATEADD(mi, 180, MIN(b.datetimestamp)) 
                  ELSE NULL END AS 'eta' 
             FROM shp_master AS a LEFT OUTER JOIN shp_status AS b ON a.serialnbr = b.serialnbr 
             WHERE a.shipto = '1022a' AND a.status = 's' AND b.status = 's' AND a.shpreq = 0 
             GROUP BY a.serialnbr, a.trailer, shipto 
             ORDER BY a.serialnbr    

不能隐式转换'System.DateTime?'& # 39; int ? & # 39;c# Linq

在您的评论中,您指定RecieveTruck.ETAint?,但您通过

分配它
 ETA = g.Key.shipto == "1026" ? (System.DateTime?)Convert.ToDateTime(g.Min(p => p.b.datetimestamp)).AddMinutes(180) :
       g.Key.shipto == "2020" ? (System.DateTime?)Convert.ToDateTime(g.Min(p => p.b.datetimestamp)).AddMinutes(195) :
       g.Key.shipto == "2017" ? (System.DateTime?)Convert.ToDateTime(g.Min(p => p.b.datetimestamp)).AddMinutes(180) : 
       g.Key.shipto == "nor" ? (System.DateTime?)Convert.ToDateTime(g.Min(p => p.b.datetimestamp)).AddMinutes(180) : null

将解析为DateTime?。你需要选择其中之一。它看起来像你正在寻找ETA是一个DateTime?,虽然,所以我只是改变属性类型,你应该很好去。