LINQ to Entities不识别方法***,因此该方法无法转换为存储表达式
我的程序里有这么一段代码:
order.OrderExpressInfo = (from oei in orderExpressRepository.Entities where oei.OrderId == order.OrderId select new EbcBuy.Bll.Orders.Models.OrderExpress { ContentInfo = oei.ContentInfo, CreatedTime = oei.CreatedTime, CreatedUserId = oei.CreatedUserId, ExpressId = oei.ExpressId, ExpressName = oei.ExpressName, ExpressOrderId = oei.ExpressOrderId, Freight = oei.Freight, Manifest = oei.Manifest, OrderId = oei.OrderId, ReceiverContactInfo = new ContactInfoModel() { ZipCode = oei.CustomerZipCode, Telephone = oei.CustomerPhone, MobilePhone = oei.CustomerTel, Address = oei.CustomerAddress, LinkMan = oei.CustomerReceiverName, ProvinceId = oei.CustomerArea.ProvinceId, ProvinceName = oei.CustomerArea.ProvinceName, CityId = oei.CustomerArea.CityId, CityName = oei.CustomerArea.CityName, CountyId = oei.CustomerArea.DistrictId, CountyName = oei.CustomerArea.DistrictName }, ShopId = order.OrderId, ShopName = order.ShopName, ShopContactInfo = new ContactInfoModel() { ZipCode = oei.ShopZipCode, Telephone = oei.ShopTel, MobilePhone = oei.ShopPhone, Address = oei.ShopAddress, LinkMan = oei.ShopLinkMan, ProvinceId = oei.ShopArea.ProvinceId, ProvinceName = oei.ShopArea.ProvinceName, CityId = oei.ShopArea.CityId, CityName = oei.ShopArea.CityName, CountyId = oei.ShopArea.DistrictId, CountyName = oei.ShopArea.DistrictName } }).FirstOrDefault();
其中,ContactInfoModel是一个实体类,定义了买家或卖家的contact信息, 实体类 EbcBuy.Bll.Orders.Models.OrderExpress
具备两个该类型的属性, ReceiverContactInfo和ShopContactInfo。 这里使用linq to entity要给这2个属性赋值, 要注意了,这里给ContactInfoModel初始化设置的属性个数和顺序必须相同。 否则,运行时会报如下异常:
“System.NotSupportedException”类型的未经处理的异常在 EntityFramework.dll 中发生
其他信息: 在单个 LINQ to Entities 查询中的两个结构上不兼容的初始化过程中出现类型“EbcBuy.Bll.Common.Models.ContactInfoModel”。类型可以在同一查询中的两个位置初始化,但前提是在这两个位置设置了相同的属性,且这些属性是以相同的顺序设置的。
我这个ContactInfoModel在项目里好多的linq to entity代码里进行这样的初始化, 为了提高复用,我把上面的初始化封装成了一个方法, 供各处调用。 修改后的代码如下:
order.OrderExpressInfo = (from oei in orderExpressRepository.Entities where oei.OrderId == order.OrderId select new EbcBuy.Bll.Orders.Models.OrderExpress { ContentInfo = oei.ContentInfo, CreatedTime = oei.CreatedTime, CreatedUserId = oei.CreatedUserId, ExpressId = oei.ExpressId, ExpressName = oei.ExpressName, ExpressOrderId = oei.ExpressOrderId, Freight = oei.Freight, Manifest = oei.Manifest, OrderId = oei.OrderId, ReceiverContactInfo = GetCustomerContactInfoModelFromPO(oei), ShopId = order.OrderId, ShopName = order.ShopName, ShopContactInfo = GetShopContactInfoModelFromPO(oei) }).FirstOrDefault();
GetCustomerContactInfoModelFromPO和GetShopContactInfoModelFromPO定义很简单,你懂的。
结果,在运行时,文章标题提到的异常出现了:
“System.NotSupportedException”类型的未经处理的异常在 EntityFramework.dll 中发生
其他信息: LINQ to Entities 不识别方法“EbcBuy.Bll.Common.Models.ContactInfoModel GetCustomerContactInfoModelFromPO(EbcBuy.Dal.Orders.Models.OrderInfo)”,因此该方法无法转换为存储表达式。
【园子里搜索了一下, 有如下结论】在LINQ to Entities 中,使用lambda或linq时,变量一定要提前转换好,可不能到lambda里或linq里再转换。如:
var o= _db.Dictionary.Where(x => x.Type.Equals(type4.ToString()));
.ToString必然产生错误,应该:
string t=type4.ToString();
var o= _db.Dictionary.Where(x => x.Type.Equals(t));
用lambda或linq时,不要尝试去转换内置类型,是不允许的,如:
string t=type4.ToString();
var o= _db.Dictionary.Where(x => Convert.ToString(x.Type).Equals(t));
Convert.ToString必然要出错.
可以考虑使用如下方式:
using System.Data.Objects.SqlClient; //在 System.Data.Entity.dll 中 //获取市级地区 public JsonResult GetCity(string id) { var city = from c in db.AreaDivide where SqlFunctions.StringConvert((double)c.ParentID) == id select new { text = c.AreaName, value = c.ID }; return Json(city.ToList(), JsonRequestBehavior.AllowGet); }
不过,像我这种情况,无法复用的话,还真是谈不上简洁性了。不晓得还有没有什么好的解决办法...
当看到一些不好的代码时,会发现我还算优秀;当看到优秀的代码时,也才意识到持续学习的重要!--buguge
本文来自博客园,转载请注明原文链接:https://www.cnblogs.com/buguge/p/3877224.html