Linq2Sql“成员访问不合法类型”例外

时间:2011-09-16 13:28:54

标签: .net linq-to-sql .net-4.0 compiled-query

我有一个带编译查询的静态类,我想重用一些子查询。所以我将一个公共部分提取到静态属性中,然后在多个查询中引用它:

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:成员访问'System.String   'UserAccount'的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)
    );

为什么呢?任何解决方法?

0 个答案:

没有答案