简体   繁体   English

LINQ to Entities 无法识别方法“System.DateTime GetValueOrDefault()”

[英]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.

相关问题 如何修复“LINQ to Entities 无法识别方法‘System.DateTime GetValueOrDefault()’”? - How to fix "LINQ to Entities does not recognize the method 'System.DateTime GetValueOrDefault()'"? LINQ to Entities无法识别方法&#39;System.DateTime ToDateTime(System.String)&#39;方法 - LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method LINQ to Entities 无法识别方法“System.TimeSpan Subtract(System.DateTime)”方法 - LINQ to Entities does not recognize the method 'System.TimeSpan Subtract(System.DateTime)' method LINQ to Entities无法识别方法&#39;System.DateTime ConvertShamsiToMiladi(System.String)&#39;方法 - LINQ to Entities does not recognize the method 'System.DateTime ConvertShamsiToMiladi(System.String)' method LINQ to Entities无法识别方法&#39;System.DateTime ToDateTime(System.String)&#39; - LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)' - LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' LINQ to Entities 无法识别方法 &#39;System.DateTime GetDate()&#39; 方法,并且此方法无法转换为存储表达式 - LINQ to Entities does not recognize the method 'System.DateTime GetDate()' method, and this method cannot be translated into a store expression LINQ to Entities无法识别方法&#39;System.TimeSpan Subtract(System.DateTime) - LINQ to Entities does not recognize the method 'System.TimeSpan Subtract(System.DateTime) LINQ to Entities无法识别方法&#39;Int32 GetMonth(System.DateTime)&#39;方法 - LINQ to Entities does not recognize the method 'Int32 GetMonth(System.DateTime)' method ASP.NET实体框架LINQ to Entities无法识别方法&#39;System.DateTime ToDateTime(System.String) - ASP.NET Entity Framework LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM