我目前正在尝试通过预编译来优化程序中的一些 LINQ 查询。 其中一些查询广泛使用了预先加载;这是一个例子:
public static Func<DatabaseEntities, string, IQueryable<Employee>> GetAllByName =
CompiledQuery.Compile<DatabaseEntities, string, IQueryable<Employee>
((context, name) => context.Employees
.Include(e => e.Email)
.Where(e => e.LastName == name));
使用示例:
var employees = GetAllByName(dbContext, "Bob").ToList();
不幸的是,尝试使用它会导致以下错误:
LINQ to Entities does not recognize the method 'System.Linq.IQueryable[Employee] Include[Employee,Email] (System.Linq.IQueryable[Employee], System.Linq.Expressions.Expression[System.Func[Employee,Email]]) ' method, and this method cannot be translated into a store expression.
我注意到预加载的常规方法 (Include(string)) 在预编译查询中工作正常。有没有办法也使用 lambda 版本?
最佳答案
简而言之,没有。
您不能在预编译的 linq 语句/查询中使用 lambda。
关于c# - 在已编译的 LINQ 查询中使用 lambda Include 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8159744/