6

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;

            }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
NoviceToDotNet
  • 10,387
  • 36
  • 112
  • 166

2 Answers2

15

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>());
ken2k
  • 48,145
  • 10
  • 116
  • 176
  • 1
    @NoviceToDotNet I provided a sample code to fix it. Calling ToList() **before** using `Enum.Parse` allows to execute it **after** the SQL request has been executed on the DB server. – ken2k Jan 16 '14 at 13:37
2

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.

Nitin Joshi
  • 1,638
  • 1
  • 14
  • 17
  • i introduced a column in the view like this..select DressingID as ID,Description,2 as CustType – NoviceToDotNet Jan 16 '14 at 13:20
  • 1
    try with xx.CustType.ToString() because as per your code 2 as CustType the value is integer and Enum.Parse function expects a string value as second parameter. Event after that it gives same error that probably LINQ doesn't allow to use Enum.Parse. – Nitin Joshi Jan 16 '14 at 13:39