none
LINQ to Entities does not recognize the method RRS feed

  • Question

  • Hi,

    I try to do comparaison with RegionID/CategorieID integer in the database and String value RegionID/CategorieID passed by parameter in the method index from the controller.

    The first time without comparison, I obtain the error  DbComparisonExpression requires arguments with comparable types.

    After a quick search I find I need to use the same kind of type. 

    I try to cast inside the query and I obtains the error 

    "LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression."

     

    announcements = announcements.Where(
       s => s.Title.Contains(searchString)
       || s.Description.Contains(searchString)
       && (s.RegionID.Equals(int.Parse(RegionID))
       && s.CategorieID.Equals(int.Parse(CategorieID)));
    

    Finally I did this code by init before the query

    the error is not as before.

       int regionId = int.Parse(RegionID);
       int categorieID = int.Parse(CategorieID);
       announcements = announcements.Where(
       s => s.Title.Contains(searchString)
       || s.Description.Contains(searchString)
       && (s.RegionID.Equals(RegionID)
       && s.CategorieID.Equals(CategorieID))); 

    Best regards,

    Alexandre

     

     



    Sunday, June 12, 2011 8:27 PM

Answers

  • Hi Alexandre;

    It's a good question and the answer is that the Entity Framework will convert your Linq query to a T-SQL command. The SQL server has no idea about a function called int.Parse( ... ) it only knows SQL statements. Therefore the The Linq procedure that converts from Linq to SQL fails to find this function and throws an exception.

    Fernando


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Sunday, June 12, 2011 8:57 PM

All replies

  • Hi,

    Maybe I wasn't clear.

    The code works except I would like to understand what is the reason to initiate at first before to use in linq query.

     

     


    Best regards, Alexandre http://alexandrebarault.wordpress.com
    Sunday, June 12, 2011 8:35 PM
  • Hi Alexandre;

    It's a good question and the answer is that the Entity Framework will convert your Linq query to a T-SQL command. The SQL server has no idea about a function called int.Parse( ... ) it only knows SQL statements. Therefore the The Linq procedure that converts from Linq to SQL fails to find this function and throws an exception.

    Fernando


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Sunday, June 12, 2011 8:57 PM