Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法隐式转换类型';System.DateTime?&x27;至';int?&x27;C#Linq_C#_Sql_Linq - Fatal编程技术网

无法隐式转换类型';System.DateTime?&x27;至';int?&x27;C#Linq

无法隐式转换类型';System.DateTime?&x27;至';int?&x27;C#Linq,c#,sql,linq,C#,Sql,Linq,代码:它突出显示了最底部的部分,我试图告诉Linq找到shipto列并将另一个表上的datetimestamp更新一定的分钟数,并告诉我无法将类型“System.DateTime”隐式转换为“int”,我不确定如何继续 var TestingLinq = (from a in db.shp_master join b in db.shp_status on a.serialnbr equals b.serialnbr into b_join

代码:它突出显示了最底部的部分,我试图告诉Linq找到shipto列并将另一个表上的datetimestamp更新一定的分钟数,并告诉我无法将类型“System.DateTime”隐式转换为“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    

在您的注释中,您指定了
receivetruck.ETA
是一个
int?
,但您是通过

 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

这将解析为一个
日期时间?
。你需要选择其中一个。看起来您正在寻找
ETA
作为
DateTime?
,因此我只需更改该属性类型,您就可以开始了。

感谢您提供的编辑帮助selman22这是一个编译器错误,不是运行时错误,对吗?听起来很像,我只是想确定一下。问题是您有一个类型为
int?
的属性,并且您正试图向其中分配一个
datetime?
。似乎
receivetruck.Shipped
receivetruck.ETA
符合此描述。它们是如何声明的?公共类ReceiveTruck{public int SerialNumber{get;set;}公共字符串?TrailerNumber{get;set;}公共日期时间{get;set;}公共int?ETA{get;set;}也许您可以向我们解释一下ETA变量的含义。是到达的相对时间(分钟)与其他时间(我在您的代码中没有看到)相比,还是到达的绝对时间(即日历日期和时间)。。。我应该把ETA=null,然后指定另一个变量来存储datetime?如果您选择的话。我对您的项目所知不多,无法告诉您如何为其设计代码。但是是的,如果你需要
ETA
类型为
int?
,并且你需要从即时if set中获得的信息,那么你应该将它拉到两个单独的属性中。是的,工作正常。。。我刚刚设置了ETA=null。。。然后在我的类中创建了一个新的var,并将其添加到=您建议的ETA案例中,它成功了!谢谢