コンパイルされたクエリを含む静的クラスがあり、サブクエリの一部を再利用したいと考えています。したがって、共通部分を静的プロパティに抽出し、複数のクエリで参照します。
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)
);
なんで?回避策はありますか?