I have a static class with compiled queries and I want to reuse some of subqueries. So i extract a common part into a static property and then reference it in multiple queries:
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)
);
}
This compiles just fine but at run-time i get
System.InvalidOperationException: Member access 'System.String IdentityName' of 'UserAccount' not legal on type 'System.Linq.IQueryable`1[UserAccount].
No exception in this case
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)
);
Why? Any workaround?