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

发布于 2024-12-05 08:26:37 字数 1294 浏览 1 评论 0原文

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

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[用户帐户]。

在这种情况下也不例外

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)
    );

为什么?有什么解决办法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文