调用.AsEnumerable()后抛出NotSupportedException

时间:2013-11-07 12:47:04

标签: linq entity-framework linq-to-entities

我有一些简单的代码可以从db中检索记录的ELMAH异常:

HealthMonitoringEntities context = new HealthMonitoringEntities();
IQueryable<ELMAH_Error> exceptions = context.ELMAH_Error;

if (filter.ToDate != null)
    exceptions = exceptions.Where(e => e.TimeUtc <= filter.ToDate.Value.AddHours(-4));

return exceptions.OrderByDescending(e => e.TimeUtc)
    .Take(filter.Size)
    .AsEnumerable()
    .Select(e => new ElmahException()
    {
        ErrorId = e.ErrorId,
        Application = e.Application,
        Host = e.Host,
        Type = e.Type,
        Source = e.Source,
        Error = e.Message,
        User = e.User,
        Code = e.StatusCode,
        TimeStamp = e.TimeUtc.AddHours(-4).ToString()
    }).ToList();

    }

我在这一行得到例外:

  TimeStamp = e.TimeUtc.AddHours(-4).ToString()

例外是:

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

当我在使用.AsEnumerable()进行投射之前调用Select()时,我的序列会被枚举,并且我会从实现IEnumerable<ELMAH_Error>的序列进行投影。鉴于此,为什么我不在我的投影中使用Linq-To-Objects API,它可以理解AddHours(),而不是仍在使用Linq-To-Entities API?

更新

Jon Skeet在这个主题上发表了一篇文章:

http://msmvps.com/blogs/jon_skeet/archive/2011/01/14/reimplementing-linq-to-objects-part-36-asenumerable.aspx

他有这个问题:

var query = db.Context 
          .Customers 
          .Where(c => some filter for SQL) 
          .OrderBy(c => some ordering for SQL) 
          .Select(c => some projection for SQL) 
          .AsEnumerable() // Switch to "in-process" for rest of query 
          .Where(c => some extra LINQ to Objects filtering) 
          .Select(c => some extra LINQ to Objects projection);

请注意,在致电AsEnumerable()后,他表示他正在切换到Linq-To-Objects。我在我的函数中做了类似的事情,但是我收到了一个Linq-To-Entities异常,我原以为我会针对Linq-To-Objects API执行。

进一步更新

来自Jim Wooley的博客:http://linqinaction.net/blogs/jwooley/archive/2009/01/21/linq-supported-data-types-and-functions.aspx

“作为示例,以下方法显示为具有DateTime值的翻译:Add,Equals,CompareTo,Date,Day,Month,Year。相比之下,不支持ToShortDateString,IsLeapYear,ToUniversalTime等方法。

如果您需要使用其中一种不受支持的方法,则需要将结果强制转换为客户端,并在此时使用LINQ to Objects对其进行评估。你可以在查询理解的任何一点使用.AsEnumerable扩展方法来做到这一点。“

这不是我正在做的吗?

1 个答案:

答案 0 :(得分:0)

您忘记保护第一个电话

if (filter.ToDate != null)
    exceptions = exceptions.AsEnumerable()
                           .Where(e => e.TimeUtc <= filter.ToDate.Value.AddHours(-4));

修改 请记住,IQueryables在枚举之前不会被转换为sql,因此您的调试器将执行该行,但在您返回之前不会发生错误。如果filter.ToDate为空,则您的代码等效于此:

if(filter.ToDate == null)
   return exceptions
      .Where(e => e.TimeUtc <= filter.ToDate
                                   .Value
                                   .AddHours(-4))  //uh-oh won't work in Linq-to-Entities
                                   .OrderByDescending(e => e.TimeUtc)
      .Take(filter.Size)
      .AsEnumerable()   //too late to protect you!
      .Select(e => new ElmahException()
      {
          ErrorId = e.ErrorId,
          Application = e.Application,
          Host = e.Host,
          Type = e.Type,
          Source = e.Source,
          Error = e.Message,
          User = e.User,
          Code = e.StatusCode,
          TimeStamp = e.TimeUtc.AddHours(-4).ToString()  //if we get this far we're OK
      }).ToList();