在扩展方法内部使用扩展方法的EF查询

问题描述 投票:1回答:2

我有表请求,具有批准:

public class Request {
    public virtual List<Approval> Approvals { get; set; } 
}

来自数据库的请求

public DbSet<Request> Request { get; set; }

我有扩展方法:

public static IQueryable<Request> HaveApprovals(this IQueryable<Request> requests) {
    return requests.Where(r => r.ActiveApprovals().Count() > 0).ToList();
}

和另一种扩展方法:

public static IEnumerable<Approval> ActiveApprovals(this Request request) {
     return request.Approvals.Where(a => !a.Deleted);
}

但出现错误:

LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable`1[Domain.Approval] ActiveApprovals(Domain.Request)' method, and this method cannot be translated into a store expression.

由于某种原因,一种扩展方法可以转换为LINQ,但是当在另一种扩展方法中使用扩展方法时,它将无法转换。有什么建议吗?

c# linq entity-framework linq-to-entities extension-methods
2个回答
0
投票

尝试通过这种方式重写您的HaveApprovals扩展名:


0
投票

返回一个Iqueryable或Ienumerable而不是列表。该查询将由sql处理,因此它无法理解返回的列表

© www.soinside.com 2019 - 2024. All rights reserved.