用于将 LINQ 代码转换为 SQL 的数据提供程序不理解ToShortDateString
。因此,您不能在发送到数据库的 LINQ 查询中使用它。从数据库返回数据后需要调用此方法:
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
};
顺便说一句:还有另一种解决方案可以生成更短的代码:
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
};
这两个版本之间的区别在于,第一个版本仅从您需要的数据库中请求那些列,而第二个版本将返回两个表的所有列。