NoviceToDotNet
NoviceToDotNet

Reputation: 10815

LINQ to Entities does not recognize the method '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;

            }

Upvotes: 6

Views: 10427

Answers (2)

ken2k
ken2k

Reputation: 49013

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>());

Upvotes: 15

Nitin Joshi
Nitin Joshi

Reputation: 1666

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.

Upvotes: 2

Related Questions