在一个项目中使用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);
}

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

最新文章

  1. window7 右键菜单显示-》在此处打开命令窗口
  2. JVM之---Java内存分配参数(第四篇)
  3. makefile debug
  4. BackgroundWorker 后台进程控制窗体label、richtextbook内容刷新
  5. Android Intent 三解决
  6. MVC3+EF4.1学习系列(九)-----EF4.1其他的一些技巧的使用
  7. C语言 extern学习2 分析
  8. wemall doraemon中Android app商城系统工具集合类,包含各种程序中用到的静态方法
  9. 读Zepto源码之assets模块
  10. Zabbix实战-简易教程(1)--总流程
  11. acm几何
  12. CentOS命令登录MySQL时,报错ERROR 1045 (28000): Access denied for user root@localhost (using password: NO)错误解决方法
  13. 了解Queue
  14. 22 【python】入门指南:函数
  15. js去除字符串空格(空白符)
  16. SRM482
  17. [HDU5714]拍照
  18. Android 7.0正式版工厂镜像下载
  19. leetcode504
  20. Codeforces Beta Round #11 A. Increasing Sequence 贪心

热门文章

  1. -_-#微信内置JavaScript API WeixinJSBridge
  2. 【宽搜】【并查集】Vijos P1015 十字绣
  3. UVa 10025: The ? 1 ? 2 ? ... ? n = k problem
  4. 【COM学习】之一、QueryInterface
  5. View Controller 生命周期的各个方法的用法
  6. soundPool和audiofocus
  7. 单页面应用SPA架构
  8. css position 应用(absolute和relative用法)
  9. maven部署命令
  10. Wcf序列化的循环引用问题1