Kyle Rickaby
Kyle Rickaby

Reputation: 117

Cannot implicitly convert type 'System.DateTime?' to 'int?' C# Linq

The Code: It highlights the bottom most part where I can trying to tell Linq to find the shipto column and update the datetimestamp on the other table by a certain amount of minutes and tells me that I cannot implicitly convert type 'System.DateTime?' to 'int?' and I am unsure of how to continue...

  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);

Gives me the error in the title.... I was trying to convert this SQL Query to 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    

Upvotes: 0

Views: 2656

Answers (1)

Matthew Haugen
Matthew Haugen

Reputation: 13286

In your comment, you specified that RecieveTruck.ETA is an int?, but you're assigning it by doing

 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

And that will resolve to a DateTime?. You'll need to choose one or the other. It looks like you're looking for ETA to be a DateTime?, though, so I'd just change that property type and you should be good to go.

Upvotes: 1

Related Questions