LINQ to Entities无法识别方法’System.Object Parse(System.Type,System.String)’

我收到此错误,我试图解决它很长但无法解决它。 LINQ to Entities无法识别方法’System.Object Parse(System.Type,System.String)’方法,并且此方法无法转换为商店表达式。

public static List 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 lstCust = new List(); 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()); return lstCust; } 

当您使用LINQ To Entities时,Entity Framework当前正在尝试将Enum.Parse转换为SQL,但它失败了,因为它不是受支持的函数。

你可以做的是在调用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()); 

我认为这个错误是针对custType = (customizationType)Enum.Parse(typeof(customizationType), xx.CustType) 。 BTW是什么类型的xx.CustType? 我认为它返回字符串,但预期的类型是枚举,这就是为什么它抛出这个错误。