LINQ to Entities无法识别方法'System.DateTime GetValueOrDefault()' [英] LINQ to Entities does not recognize the method 'System.DateTime GetValueOrDefault()'

查看:160
本文介绍了LINQ to Entities无法识别方法'System.DateTime GetValueOrDefault()'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在非常简单的代码中苦苦挣扎,而在其他类中类似的代码却无法工作.如果删除GetValueOrDefault(),它将无法编译.我也在使用System.Linq. 我收到此运行时错误:LINQ to Entities无法识别方法'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(). Also I am using System.Linq. I'm getting this runtime error: LINQ to Entities does not recognize the method '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;
    }


为什么讨论了Linq-to-Entites无法翻译.ToString()方法但在此处建议的方法的类似问题-根本不使用method或获取Microsoft修复它不适用于这种情况.


Why would Entity Framework not be able to use ToString() in a LINQ statement? 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.

推荐答案

您应该能够使用空合并运算符??:

You should be able to use the Null Coalescing operator ??:

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(); 

这篇关于LINQ to Entities无法识别方法'System.DateTime GetValueOrDefault()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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