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

后端 未结 11 2196
花落未央
花落未央 2020-11-22 10:41

I\'m migrating some stuff from one mysql server to a sql server but i can\'t figure out how to make this code work:

using (var context = new Context())
{
            


        
11条回答
  •  失恋的感觉
    2020-11-22 11:21

    As others have answered, this breaks because .ToString fails to translate to relevant SQL on the way into the database.

    However, Microsoft provides the SqlFunctions class that is a collection of methods that can be used in situations like this.

    For this case, what you are looking for here is SqlFunctions.StringConvert:

    from p in context.pages
    where  p.Serial == SqlFunctions.StringConvert((double)item.Key.Id)
    select p;
    

    Good when the solution with temporary variables is not desirable for whatever reasons.

    Similar to SqlFunctions you also have the EntityFunctions (with EF6 obsoleted by DbFunctions) that provides a different set of functions that also are data source agnostic (not limited to e.g. SQL).

提交回复
热议问题