使用DATETIMEOFFSET时,LINQ到实体框架错误 [英] Linq to entity framework error when using datetimeoffset

查看:189
本文介绍了使用DATETIMEOFFSET时,LINQ到实体框架错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时得到一个奇怪的错误,我不能修复它。有人可以帮忙吗?



由于它dosent像下面的代码失败 o.ordered.DateTime.ToShortDateString()(它的工作原理时部分被注释掉)。 o.ordered DATETIMEOFFSET 。有错误时给出了如下。我已经尝试了一些不同势的版本就像使用日期的toString ,而不是 toshortdatestring

  LINQ到实体无法识别方法'System.String ToShortDateString()方法,这种方法不能被翻译成店的表情。 

变种BeOrders邻在BEdb.onlineOrders =
在BEdb.order_Statuses的JOIN
上o.status等于s.ID
,其中o.custCode ==页.AccountID
选择新DataLayer.OrderStatusItem {
=城市o.city,
customersOrderRef = o.customersOrderRef,
日期=(o.actualDelivery ?? o.plannedDelivery),
= DATE1(o.actualCease ?? o.actualCease),
号= o.number,
下令= o.ordered.DateTime.ToShortDateString(),
邮编= O。邮编,
状态= s.status,
stockCode = o.stockCode,
UpdatedByAccount = o.UpdatedByAccount
};


解决方案

数据提供者用来翻译LINQ代码为SQL不明白 ToShortDateString 。正因为如此,不能在被发送到数据库LINQ查询使用它。你需要调用这个方法后的数据已经从数据库返回的:

  VAR BeOrders =(从o在BEdb.onlineOrders 
在BEdb.order_Statuses
上o.status的JOIN等于s.ID
,其中o.custCode == pp.AccountID
选择新{
城市= O。城市,
customersOrderRef = o.customersOrderRef,
日期=(o.actualDelivery ?? o.plannedDelivery),
DATE1 =(o.actualCease ?? o.actualCease),
数= o.number,
下令= o.ordered,
邮编= o.postCode,
状态= s.status,
stockCode = o.stockCode,
UpdatedByAccount = o.UpdatedByAccount
})了ToList()
。选择(X =>新DataLayer.OrderStatusItem {
=城市x.city,
customersOrderRef = x.customersOrderRef,
日期= x.date,
DATE1 = x.date1,
号= x.number,
下令= x.ordered.DateTime.ToShortDateString(),
邮编= x.postCode,
状态= x.status,
stockCode = x.stockCode ,
UpdatedByAccount = x.UpdatedByAccount
};



BTW:有产生短代码另一种解决方案:

  VAR BeOrders =(邻在BEdb.onlineOrders 
的JOIN在BEdb.order_Statuses
上o.status等于s.ID
其中,o.custCode == pp.AccountID
选择新的{O,S})了ToList()
。选择(X =方式>新DataLayer.OrderStatusItem
{
城市= xocity,
customersOrderRef = xocustomersOrderRef,
日期=(xoactualDelivery ?? xoplannedDelivery),
DATE1 =(xoactualCease ?? xoactualCease),
号= XO号,
下令= xoordered.DateTime.ToShortDateString(),
邮编= xopostCode,
状态= xsstatus,
stockCode = xostockCode,
UpdatedByAccount = xoUpdatedByAccount
};



这两个版本之间的差别在于,第一个版本只从数据库请求那些列需要的地方作为第二个版本将返回两个表中的所有列。


Im getting an odd error and I cant fix it. Can someone help?

The below code fails because it dosent like o.ordered.DateTime.ToShortDateString() (it works when that part is commented out). o.ordered is a datetimeoffset. There error it gives is below. I have tried a few diffrent versions like using date and tostring rather than toshortdatestring.

LINQ to Entities does not recognize the method 'System.String ToShortDateString()' method, and this method cannot be translated into a store expression. 

        var BeOrders = from o in BEdb.onlineOrders
                       join s in BEdb.order_Statuses
                       on o.status equals s.ID
                       where o.custCode == pp.AccountID
                       select new DataLayer.OrderStatusItem {
                           city = o.city,
                           customersOrderRef = o.customersOrderRef,
                           date = (o.actualDelivery ?? o.plannedDelivery),
                           date1 = (o.actualCease ?? o.actualCease),
                           number = o.number,
                           ordered = o.ordered.DateTime.ToShortDateString(),
                           postCode = o.postCode,
                           status = s.status,
                           stockCode = o.stockCode,
                           UpdatedByAccount = o.UpdatedByAccount
                       };

解决方案

The data provider used to translate the LINQ code to SQL doesn't understand ToShortDateString. Because of that, you can't use it in a LINQ query that is sent to the database. You need to call this method after the data has been returned from the database:

    var BeOrders = (from o in BEdb.onlineOrders
                   join s in BEdb.order_Statuses
                   on o.status equals s.ID
                   where o.custCode == pp.AccountID
                   select new {
                       city = o.city,
                       customersOrderRef = o.customersOrderRef,
                       date = (o.actualDelivery ?? o.plannedDelivery),
                       date1 = (o.actualCease ?? o.actualCease),
                       number = o.number,
                       ordered = o.ordered,
                       postCode = o.postCode,
                       status = s.status,
                       stockCode = o.stockCode,
                       UpdatedByAccount = o.UpdatedByAccount
                   }).ToList()
                     .Select(x => new DataLayer.OrderStatusItem {
                       city = x.city,
                       customersOrderRef = x.customersOrderRef,
                       date = x.date,
                       date1 = x.date1,
                       number = x.number,
                       ordered = x.ordered.DateTime.ToShortDateString(),
                       postCode = x.postCode,
                       status = x.status,
                       stockCode = x.stockCode,
                       UpdatedByAccount = x.UpdatedByAccount
                   };

BTW: There is another solution that produces shorter code:

    var BeOrders = (from o in BEdb.onlineOrders
                   join s in BEdb.order_Statuses
                   on o.status equals s.ID
                   where o.custCode == pp.AccountID
                   select new { o, s }).ToList()
                   .Select(x => new DataLayer.OrderStatusItem
                   {
                       city = x.o.city,
                       customersOrderRef = x.o.customersOrderRef,
                       date = (x.o.actualDelivery ?? x.o.plannedDelivery),
                       date1 = (x.o.actualCease ?? x.o.actualCease),
                       number = x.o.number,
                       ordered = x.o.ordered.DateTime.ToShortDateString(),
                       postCode = x.o.postCode,
                       status = x.s.status,
                       stockCode = x.o.stockCode,
                       UpdatedByAccount = x.o.UpdatedByAccount
                   };

The difference between those two versions is that the first version only requests those columns from the database you need where as the second version will return all columns of the two tables.

这篇关于使用DATETIMEOFFSET时,LINQ到实体框架错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆