[英]LINQ to Entities does not recognize the method 'System.Object Parse(System.Type, System.String)'
I am getting this error, i am trying to resolve it long but unable to fix it. 我收到此错误,我试图解决它很长但无法解决它。 LINQ to Entities does not recognize the method 'System.Object Parse(System.Type, System.String)' method, and this method cannot be translated into a store expression.
LINQ to Entities无法识别方法'System.Object Parse(System.Type,System.String)'方法,并且此方法无法转换为商店表达式。
public static List<itmCustomization> GetAllProductCustomziation(string catID)
{
var arrcatID = catID.Split('_');
int PId = int.Parse(arrcatID[0].ToString());
int CatID = int.Parse(arrcatID[1].ToString());
EposWebOrderEntities db = new EposWebOrderEntities();
List<itmCustomization> lstCust = new List<itmCustomization>();
lstCust.AddRange((from xx in db.vw_AllCustomization
where xx.CatID == CatID && xx.ProductID == PID
select new itmCustomization()
{
itmName = xx.Description,
catId = (int)xx.CatID,
proId = (int)xx.ProductID,
custType = (customizationType)Enum.Parse(typeof(customizationType), xx.CustType)
}).ToList<itmCustomization>());
return lstCust;
}
As you're using LINQ To Entities, Entity Framework is currently trying to translate Enum.Parse
into SQL, and it fails, because it's not a supported function. 当您使用LINQ To Entities时,Entity Framework目前正在尝试将
Enum.Parse
转换为SQL,但它失败了,因为它不是受支持的函数。
What you could do is materialize your SQL request before calling Enum.Parse
: 你可以做的是在调用
Enum.Parse
之前实现你的SQL请求:
lstCust.AddRange((from xx in db.vw_AllCustomization
where xx.CatID == CatID && xx.ProductID == PID
select xx)
.TolList() // Moves to LINQ To Object here
.Select(xx => new itmCustomization()
{
itmName = xx.Description,
catId = (int)xx.CatID,
proId = (int)xx.ProductID,
custType = (customizationType)Enum.Parse(typeof(customizationType), xx.CustType)
}).ToList<itmCustomization>());
I think this error is being throw for custType = (customizationType)Enum.Parse(typeof(customizationType), xx.CustType)
. 我认为这个错误正在抛出
custType = (customizationType)Enum.Parse(typeof(customizationType), xx.CustType)
。 BTW what is the type of xx.CustType? BTW是什么类型的xx.CustType? I thing it is returning string but the expected type is an enum thats why it is throwing this error.
我认为它返回字符串,但预期的类型是枚举,这就是为什么它抛出这个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.