LINQ to Entities无法识别'System.DateTime AddMinutes(Double)'方法

时间:2014-07-15 12:27:47

标签: c# linq

运行时错误,说明标题显示的内容...... 我的Linq在我的控制器中如下:

     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 = null,
                                   ShipTo = 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);

然后在我看来,我有以下内容:

@model IEnumerable<TestingMainPage.Models.RecieveTruck>
@if (Model.Count() > 1 )
        {
            foreach (var tl in Model)
            {              
            <tr>
                <td align="center" valign="middle" nowrap="nowrap" class="bodyTextTL"><img src="~/Content/images/view.gif" alt="View Load" style="cursor:pointer;" onclick="document.location.href = '/RecieveTruck/ViewLoad?LoadID=' + @tl.SerialNumber + ''" /></td>
                <td align="center" valign="middle" nowrap="nowrap" class="bodyTextTL">@tl.SerialNumber</td>
                <td align="center" valign="middle" nowrap="nowrap" class="bodyTextTL">@tl.TrailerNumber</td>
                <td align="center" valign="middle" nowrap="nowrap" class="bodyTextTL">@tl.Shipped</td>
                <td align="center" valign="middle" nowrap="nowrap" class="bodyTextTL">@tl.ETA</td>
                <td align="center" valign="middle" nowrap="nowrap" class="bodyTextTLR">
                    <input type="button" name="Receive Load" value="Receive Load" style="cursor: pointer;" class="bodyText6" onmouseover="this.style.color='orangered'; this.style.fontWeight='bold';" onmouseout="    this.style.color='black'; this.style.fontWeight='normal';" onclick="document.location.href = '/RecieveTruck/Execute'" />&nbsp
                </td>
            </tr>
            }
        }      

所以我的问题是,如何将其传递到我的视图中,以便我可以使用它来填充我在网络程序中创建的表格。 我理解为什么在运行它之前它不会给我一个错误,但我怎么想把它传递给我的观点呢?

2 个答案:

答案 0 :(得分:2)

无需在数据库本身上运行该逻辑 - 您可以在选择了必要的数据后在应用程序服务器上运行它。

因此,请将查询更改为仅选择原始shipTo值。然后调用AsEnumerable(),它会弹回标准的LINQ到对象,并在那里计算你的ShipTo值。

TestingLinq = (from 
...
select new {
                               SerialNumber = g.Key.serialnbr,
                               TrailerNumber = (g.Key.trailer ?? "N/A"),
                               Shipped = g.Min(p => p.b.datetimestamp),
                               ETA = null,
                               Timestamp = p.b.datetimestamp,
                               ShipTo = g.Key.shipto
})
  .AsEnumerable()
  .Select(x => new ReceiveTruck { 
          SerialNumber = x.SerialNumber,
          ...
          ShipTo = CalculateShipTo(x.Timestamp, x.ShipTo)
  })

我省略了CalculateShipTo的实施,但你应该明白这一点。

答案 1 :(得分:1)

您可以使用EntityFunctions。它在System.Data.Entity.Core.Objects

EntityFunctions.AddDays(DateTime, num);