【问题标题】:LINQ to Entities does not recognize 'System.DateTime AddMinutes(Double)' method [duplicate]LINQ to Entities 无法识别“System.DateTime AddMinutes(Double)”方法 [重复]
【发布时间】:2014-07-15 12:27:47
【问题描述】:

运行时错误,说明标题显示的内容... 我的 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>
            }
        }      

所以我的问题是,我如何将它传递给我的视图,以便我可以使用它来填充我在我的网络程序上创建的表格.. 我明白为什么它在运行它之前不会给我一个错误,但是我应该如何将它传递给我的视图?

【问题讨论】:

标签: c# linq


【解决方案1】:

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

因此,将您的查询更改为只选择原始的shipTo 值。然后调用AsEnumerable(),它将把你弹回标准的LINQ-to-objects,并在那里计算你的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 的实现,但你应该明白了。

【讨论】:

  • 是我在数据库上运行该逻辑导致错误的原因吗?或者这只是一条替代路线?此外,p 不在那个上下文中,b 也不是你的时间戳,所以即使我将它添加到模块中(我假设只是使用 calculateShipTo)会给我另一个错误......这就像我的第二周使用 linq,这是一个超级复杂的(对我来说)查询,所以我想我不明白......
  • 抱歉拖了这么久才回复,最后在这个中间的另一个项目上......
【解决方案2】:

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

EntityFunctions.AddDays(DateTime, num);

【讨论】:

  • 我假设你的意思是这样的?
  • ShipTo = g.Key.shipto == "1026" ? EntityFunctions.AddMinutes(DateTime, 180) :因为当我这样做时它告诉我......'System.Data.Entity.Core.Objects.EntityFunctions' 已过时并已被 System.Data.Entity.DbFunctions 取代,我无法添加,它还告诉我 DateTime 是一种“类型”,但用作变量
  • 抱歉拖了这么久才回复,最后在这个中间的另一个项目上......
  • ShipTo = g.Key.shipto == "1026" ? System.Data.Entity.DbFunctions.AddMinutes("DATETIMEHERE", 180) :
猜你喜欢
  • 1970-01-01
  • 2015-10-12
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
相关资源
最近更新 更多