Linq2Sql“成员访问类型不合法”例外
我有一个带有编译查询的静态类,我想重用一些子查询。因此,我将公共部分提取到静态属性中,然后在多个查询中引用它:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论