我正在尝试通过预编译来优化程序中的一些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));
Run Code Online (Sandbox Code Playgroud)
使用示例:
var employees = GetAllByName(dbContext, "Bob").ToList();
Run Code Online (Sandbox Code Playgroud)
不幸的是,尝试使用它会导致以下错误:
LINQ to Entities无法识别方法'System.Linq.IQueryable [Employee] Include [Employee,Email](System.Linq.IQueryable [Employee],System.Linq.Expressions.Expression [System.Func [Employee,Email]] )'方法,并且此方法无法转换为商店表达式.
我注意到,正常的加载加载方法(Include(string))在预编译查询中工作正常.有没有办法使用lambda版本?
c# linq-to-entities self-tracking-entities entity-framework-4.1