无法将类型“System.Nullable`1”强制转换为类型“System.Object”。LINQ to Entities 仅支持强制转换 EDM 基元或枚举类型。
2024-10-18 18:13:05
在一个项目中使用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);
}
这其中的原因,现在还不了解,等了解后再作补充。
最新文章
- window7 右键菜单显示-》在此处打开命令窗口
- JVM之---Java内存分配参数(第四篇)
- makefile debug
- BackgroundWorker 后台进程控制窗体label、richtextbook内容刷新
- Android Intent 三解决
- MVC3+EF4.1学习系列(九)-----EF4.1其他的一些技巧的使用
- C语言 extern学习2 分析
- wemall doraemon中Android app商城系统工具集合类,包含各种程序中用到的静态方法
- 读Zepto源码之assets模块
- Zabbix实战-简易教程(1)--总流程
- acm几何
- CentOS命令登录MySQL时,报错ERROR 1045 (28000): Access denied for user root@localhost (using password: NO)错误解决方法
- 了解Queue
- 22 【python】入门指南:函数
- js去除字符串空格(空白符)
- SRM482
- [HDU5714]拍照
- Android 7.0正式版工厂镜像下载
- leetcode504
- Codeforces Beta Round #11 A. Increasing Sequence 贪心
热门文章
- -_-#微信内置JavaScript API WeixinJSBridge
- 【宽搜】【并查集】Vijos P1015 十字绣
- UVa 10025: The ? 1 ? 2 ? ... ? n = k problem
- 【COM学习】之一、QueryInterface
- View Controller 生命周期的各个方法的用法
- soundPool和audiofocus
- 单页面应用SPA架构
- css position 应用(absolute和relative用法)
- maven部署命令
- Wcf序列化的循环引用问题1