我收到此错误,我试图长时间解决它,但无法修复。
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;

            }

最佳答案

当您使用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<itmCustomization>());

关于c# - LINQ to Entities无法识别 'System.Object Parse(System.Type, System.String)'方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21162373/

10-17 02:48