Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to Entities does not recognize the method 'System.Object Parse(System.Type, System.String)'

Tags:

c#

linq

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;

            }
like image 523
NoviceToDotNet Avatar asked Jan 16 '14 12:01

NoviceToDotNet


2 Answers

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.

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>());
like image 125
ken2k Avatar answered Nov 10 '22 04:11

ken2k


I think this error is being throw for custType = (customizationType)Enum.Parse(typeof(customizationType), xx.CustType). BTW what is the type of xx.CustType? I thing it is returning string but the expected type is an enum thats why it is throwing this error.

like image 33
Nitin Joshi Avatar answered Nov 10 '22 05:11

Nitin Joshi