gpt4 book ai didi

c# - LINQ to Entities 无法识别方法 'System.DateTime GetValueOrDefault()'

转载 作者:IT王子 更新时间:2023-10-29 04:16:04 24 4
gpt4 key购买 nike

努力使用非常简单的代码,这些代码在其他类中可以使用类似代码的地方不起作用。如果我删除 GetValueOrDefault(),它将无法编译。我也在使用 System.Linq。我收到此运行时错误:LINQ to Entities 无法识别方法“System.DateTime GetValueOrDefault()”。有任何想法吗?

    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-to-Entites 无法翻译 .ToString() 方法的类似问题,但那里建议的方法 - 根本不使用方法或让 Microsoft 修复它对这种情况不起作用。

最佳答案

您应该能够使用 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();

关于c# - LINQ to Entities 无法识别方法 'System.DateTime GetValueOrDefault()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26684388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com