我有一个带有编译查询的静态类,我想重用一些子查询。所以我将一个公共部分提取到一个静态属性中,然后在多个查询中引用它:
public static class Query {
// common part
static Func<MyDataContext, string, IQueryable<UserAccount>> accounts =
(db, cID) => db.UserAccount
.Where(x => x.XRef.cID == cID && bla-bla-bla);
// one of queries that reuse 'accounts' part
public static readonly Func<MyDataContext, string, string, bool> CheckClientIdentity =
CompiledQuery.Compile<MyDataContext, string, string, bool>(
(db, cID, identityName) => accounts(db, cID)
.Any(x => x.IdentityName == identityName)
);
}
这编译得很好,但在运行时我得到
System.InvalidOperationException:“UserAccount”的成员访问“System.String IdentityName”在“System.Linq.IQueryable`1[UserAccount] 类型上不合法。
在这种情况下也不例外
public static readonly Func<MyDataContext, string, string, bool> CheckClientIdentity =
CompiledQuery.Compile<MyDataContext, string, string, bool>(
(db, cID, identityName) => db.UserAccount
.Where(x => x.XRef.cID == cID && bla-bla-bla)
.Any(x => x.IdentityName == identityName)
);
为什么?任何解决方法?