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

时间:2014-01-16 12:53:17

标签: 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;

            }

2 个答案:

答案 0 :(得分:13)

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

答案 1 :(得分:2)

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