3

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?

UserControl
  • 14,766
  • 20
  • 100
  • 187
  • Is `UserAccount.IdentityName` non-public perhaps? – Jon Skeet Sep 16 '11 at 13:32
  • Is `UserAccount.IdentityName` defined in a partial class declaration (i.e. not part of the datamodel)? – Doctor Jones Sep 16 '11 at 13:35
  • It's public, auto-generated by SqlMetal property. UserAccount class is defined in the same assembly where Query class is defined. – UserControl Sep 16 '11 at 13:39
  • possible duplicate of [How to reuse where clauses in Linq To Sql queries](http://stackoverflow.com/questions/4952602/how-to-reuse-where-clauses-in-linq-to-sql-queries) – UserControl Sep 19 '11 at 06:00

0 Answers0