locked
LINQ to Entities does not recognize the method , and this method cannot be translated into a store expression. RRS feed

  • Question

  • User-446805408 posted

    i write  this code

      List<PostToXml> sampleposts = (from p in db.Posts
                                               select new PostToXml()
                                               {
                                                   description = p.PostContent.Substring(0, 150),
                                                   link = "http://" + Request.Url.Host + "/Posts/SinglePost/" +p.PostID+"/"+Seo.GenerateSlug(p.PostTitle)  , 
                                                   pubDate = p.PostDate,
                                                   title = p.PostTitle
    
                                               }).ToList();



    Error below occurs In the running time:

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

    Seo.GenerateSlug method used for create frindly url

    Thursday, April 14, 2016 1:49 PM

Answers

  • User1559292362 posted

    Hi djary,

    Error below occurs In the running time:

    You couldn't use these conversion method in a LINQ to Entities statement, they cannot be translated to SQL, you need to do the conversions in memory. like this:

    List<PostToXml> sampleposts = (from p in db.Posts
                                     select p).AsEnumerable().Select(x => new PostToXml()
                                               {
                                                   description = p.PostContent.Substring(0, 150),
                                                   link = "http://" + Request.Url.Host + "/Posts/SinglePost/" +p.PostID+"/"+Seo.GenerateSlug(p.PostTitle)  , 
                                                   pubDate = p.PostDate,
                                                   title = p.PostTitle
    
                                               }).ToList();
    

    Best regards,

    Cole Wu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 15, 2016 5:00 AM

All replies

  • User1559292362 posted

    Hi djary,

    Error below occurs In the running time:

    You couldn't use these conversion method in a LINQ to Entities statement, they cannot be translated to SQL, you need to do the conversions in memory. like this:

    List<PostToXml> sampleposts = (from p in db.Posts
                                     select p).AsEnumerable().Select(x => new PostToXml()
                                               {
                                                   description = p.PostContent.Substring(0, 150),
                                                   link = "http://" + Request.Url.Host + "/Posts/SinglePost/" +p.PostID+"/"+Seo.GenerateSlug(p.PostTitle)  , 
                                                   pubDate = p.PostDate,
                                                   title = p.PostTitle
    
                                               }).ToList();
    

    Best regards,

    Cole Wu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 15, 2016 5:00 AM
  • User-446805408 posted

    Thank you very much

    Saturday, April 16, 2016 12:38 PM