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

后端 未结 11 2012
花落未央
花落未央 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

    Just turn the LINQ to Entity query into a LINQ to Objects query (e.g. call ToArray) anytime you need to use a method call in your LINQ query.

    0 讨论(0)
  • 2020-11-22 11:22

    Cast table to Enumerable, then you call LINQ methods with using ToString() method inside:

        var example = contex.table_name.AsEnumerable()
    .Select(x => new {Date = x.date.ToString("M/d/yyyy")...)
    

    But be careful, when you calling AsEnumerable or ToList methods because you will request all data from all entity before this method. In my case above I read all table_name rows by one request.

    0 讨论(0)
  • 2020-11-22 11:24

    I got the same error in this case:

    var result = Db.SystemLog
    .Where(log =>
        eventTypeValues.Contains(log.EventType)
        && (
            search.Contains(log.Id.ToString())
            || log.Message.Contains(search)
            || log.PayLoad.Contains(search)
            || log.Timestamp.ToString(CultureInfo.CurrentUICulture).Contains(search)
        )
    )
    .OrderByDescending(log => log.Id)
    .Select(r => r);
    

    After spending way too much time debugging, I figured out that error appeared in the logic expression.

    The first line search.Contains(log.Id.ToString()) does work fine, but the last line that deals with a DateTime object made it fail miserably:

    || log.Timestamp.ToString(CultureInfo.CurrentUICulture).Contains(search)
    

    Remove the problematic line and problem solved.

    I do not fully understand why, but it seems as ToString() is a LINQ expression for strings, but not for Entities. LINQ for Entities deals with database queries like SQL, and SQL has no notion of ToString(). As such, we can not throw ToString() into a .Where() clause.

    But how then does the first line work? Instead of ToString(), SQL have CAST and CONVERT, so my best guess so far is that linq for entities uses that in some simple cases. DateTime objects are not always found to be so simple...

    0 讨论(0)
  • 2020-11-22 11:25

    The problem is that you are calling ToString in a LINQ to Entities query. That means the parser is trying to convert the ToString call into its equivalent SQL (which isn't possible...hence the exception).

    All you have to do is move the ToString call to a separate line:

    var keyString = item.Key.ToString();
    
    var pages = from p in context.entities
                where p.Serial == keyString
                select p;
    
    0 讨论(0)
  • 2020-11-22 11:29

    Upgrading to Entity Framework Version 6.2.0 worked for me.

    I was previously on Version 6.0.0.

    Hope this helps,

    0 讨论(0)
提交回复
热议问题