zoukankan      html  css  js  c++  java
  • 无法将类型“System.Nullable`1”强制转换为类型“System.Object”。LINQ to Entities 仅支持强制转换 EDM 基元或枚举类型。

     在一个项目中使用LINQ和EF时出现了题目所示的异常,搜索了很多资料都找不到解决办法,主要是因为EF方面的知识欠缺。

     先将情况记录如下,以供以后参考。

     查询主要设计两张表,由外键关联:

      

    在进行下面的查询时,出现异常:无法将类型“System.Nullable`1”强制转换为类型“System.Object”。LINQ to Entities 仅支持强制转换 EDM 基元或枚举类型。

     public ActionResult GetIpSegments()
            {
                //List<Ipsegment> ipsegments = (from s in deviceDB.Ipsegment select s).ToList();
                var ipsegments = from d in deviceDB.DeviceCategory
                                 join s in deviceDB.Ipsegment
                                 on d.devicecategoryid equals s.devicecategoryid
                                 select new
                                            {
                                                devicecategoryid1 = s.devicecategoryid,
                                                devicecategoryname1 = d.devicecategoryname,
                                                ipsegment = "202.115.242." + s.ip_head + "-202.115.242." + s.ip_end
                                            };
    
                return Json(ipsegments, JsonRequestBehavior.AllowGet);
            }

     后来,对查询做了修改,才成功。修改后的查询如下所示:

        public ActionResult GetIpSegments()
            {
                var ipsegments = from s in deviceDB.Ipsegment
                                 select new
                                 {
                                     s.devicecategoryid,
                                     s.DeviceCategory.devicecategoryname,
                                     s.ip_head,
                                     s.ip_end
                                 };
    
                return Json(ipsegments, JsonRequestBehavior.AllowGet);
            }

    这其中的原因,现在还不了解,等了解后再作补充。

  • 相关阅读:
    MFC 时钟 计算器 日期天数计算
    test10
    test9
    iOS 防止按钮快速点击造成多次响应的避免方法
    NSBundle读取图片 plist文件和txt文件
    按指定格式的子字符串,删除和分割字符串
    python批处理入门知识点
    命令行ffmpeg批量旋转视频
    NSData转化成十六进制字符串
    xcode里面使用Memory Leaks和Instruments检测内存泄漏
  • 原文地址:https://www.cnblogs.com/wangyihome/p/5552310.html
Copyright © 2011-2022 走看看