[英]LINQ to Entities does not recognize the method 'System.DateTime GetValueOrDefault()'
Struggling with very simple code that isn't working where similar code is working in other classes.与非常简单的代码作斗争,这些代码在其他类中工作的类似代码不起作用。 It won't compile if I remove GetValueOrDefault().
如果我删除 GetValueOrDefault(),它将无法编译。 Also I am using System.Linq.
我也在使用 System.Linq。 I'm getting this runtime error: LINQ to Entities does not recognize the method 'System.DateTime GetValueOrDefault()'.
我收到此运行时错误:LINQ to Entities 无法识别方法“System.DateTime GetValueOrDefault()”。 Any ideas?
有任何想法吗?
public List<AddressModel> GetAll(string customer_number)
{
addresses = (from a in db.ADDRESS
where a.CUSTOMER_NUMBER.Equals(customer_number)
select new AddressModel
{
Address_Id = a.ADDRESS_ID,
System_Id = a.SYSTEM_ID,
Customer_Number = a.CUSTOMER_NUMBER,
Address_Type = a.ADDRESS_TYPE,
Changed_On = a.CHANGED_ON.GetValueOrDefault(DateTime.MinValue),
Created_On = a.CREATED_ON.GetValueOrDefault(DateTime.MinValue),
Email = a.EMAIL
}).ToList();
return addresses;
}
Why would Entity Framework not be able to use ToString() in a LINQ statement? 为什么实体框架不能在 LINQ 语句中使用 ToString()? discusses similar problem of Linq-to-Entites not able to translate
.ToString()
method, but approaches suggested there - not using method at all or getting Microsoft to fix it did not work for this case.讨论了 Linq-to-Entites 无法翻译
.ToString()
方法的类似问题,但那里建议的方法 - 根本不使用方法或让 Microsoft 修复它在这种情况下不起作用。
You should be able to use the Null Coalescing operator ??
您应该能够使用Null Coalescing运算符
??
: :
addresses = (from a in db.ADDRESS
where a.CUSTOMER_NUMBER.Equals(customer_number)
select new AddressModel
{
Address_Id = a.ADDRESS_ID,
System_Id = a.SYSTEM_ID,
Customer_Number = a.CUSTOMER_NUMBER,
Address_Type = a.ADDRESS_TYPE,
Changed_On = a.CHANGED_ON ?? DateTime.MinValue,
Created_On = a.CREATED_ON ?? DateTime.MinValue,
Email = a.EMAIL
}).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.