LINQ to Entities无法识别方法'System.Object Parse(System.Type,System.String)' [英] LINQ to Entities does not recognize the method 'System.Object Parse(System.Type, System.String)'

查看:62
本文介绍了LINQ to Entities无法识别方法'System.Object Parse(System.Type,System.String)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到此错误,我试图长时间解决它,但无法修复. LINQ to Entities无法识别方法'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.

 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,但失败,因为它不受支持.

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.

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

What you could do is materialize your SQL request before calling 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>());

这篇关于LINQ to Entities无法识别方法'System.Object Parse(System.Type,System.String)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆