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

Nov*_*Net 4 c# linq

我收到此错误,我试图解决它很长但无法解决它.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;

            }
Run Code Online (Sandbox Code Playgroud)

ken*_*n2k 13

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

你可以做的是在调用之前实现你的SQL请求Enum.Parse:

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>());
Run Code Online (Sandbox Code Playgroud)