gpt4 book ai didi

c# - NHibernate QueryOver WithSubQuery WhereAll 子句

转载 作者:太空狗 更新时间:2023-10-30 01:05:47 25 4
gpt4 key购买 nike

休斯顿,我们有问题...不,我想是我一个人。 :)

我正在尝试对 NHibernate QueryOver 对象执行子查询。没有 SubQueries 的查询工作正常,但是当添加 SubQuery 时,一切都崩溃了。

编辑 1:
我想要完成的是选择所有与容器的所有关系都将 AccountStatus 属性设置为 DELETED 的用户

var queryOver = session.QueryOver<User>();

queryOver.WhereRestrictionOn(x => x.UserHasContainer).IsNotEmpty
.JoinQueryOver<ContainerUser>(x => x.UserHasContainer)
.WithSubquery.WhereAll(x => x.AccountStatus == AccountStatus.DELETED);

不断得到这个异常:

System.Exception: right operand should be detachedQueryInstance.As<T>() - "DELETED"
at NHibernate.Impl.ExpressionProcessor.FindDetachedCriteria(Expression expression)
at NHibernate.Impl.ExpressionProcessor.ProcessSubqueryExpression(LambdaSubqueryType subqueryType, BinaryExpression be)
at NHibernate.Impl.ExpressionProcessor.ProcessSubquery[T](LambdaSubqueryType subqueryType, Expression`1 expression)
at NHibernate.Criterion.Subqueries.WhereAll[T](Expression`1 expression)
at RBAC.Infrastructure.DataAccess.QueryObject.Implementation.UserQueryObject.FilterByContainerRelationsAreScheduledForDeletion(IQueryOver`2 queryOver) in c:\APPL\dev\RBAC\Dev\RBAC.Infrastructure\DataAccess\QueryObject\Implementation\UserQueryObject.cs:line 113
at RBAC.Infrastructure.DataAccess.QueryObject.Implementation.UserQueryObject._ExecuteQuery(IQueryOver`2 queryOver) in c:\APPL\dev\RBAC\Dev\RBAC.Infrastructure\DataAccess\QueryObject\Implementation\UserQueryObject.cs:line 101
at RBAC.Infrastructure.DataAccess.QueryObject.Base.BaseQueryObject`1.ExecuteQuery(ISessionDecorater session) in c:\APPL\dev\RBAC\Dev\RBAC.Infrastructure\DataAccess\QueryObject\Base\BaseQueryObject.cs:line 20
at RBAC.Infrastructure.DataAccess.GenericDAO.Implementation.GenericDAO.GetByQueryObject[T](IQueryObject`1 queryObject) in c:\APPL\dev\RBAC\Dev\RBAC.Infrastructure\DataAccess\GenericDAO\Implementation\GenericDAO.cs:line 253
at RBAC.Infrastructure.BusinessService.CleanupModule.Implementation.RBACCleanerActions.CleanUpUsers() in c:\APPL\dev\RBAC\Dev\RBAC.Infrastructure\BusinessService\CleanupModule\Implementation\RBACCleanerActions.cs:line 59

我在谷歌上、下和侧面搜索了right operand should be detachedQueryInstance 和其他查询,但没有成功。希望这里有人知道出了什么问题,以及将来如何解决这些问题。

我也试过这个查询但没有成功:

queryOver.WhereRestrictionOn(x => x.UserHasContainer).IsNotEmpty
.JoinQueryOver<ContainerUser>(x => x.UserHasContainer)
.Where(Subqueries.WhereAll<ContainerUser>(x => x.AccountStatus == AccountStatus.DELETED));

异常(exception):

System.InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.PropertyExpression' to type 'System.Linq.Expressions.BinaryExpression'.
at NHibernate.Impl.ExpressionProcessor.ProcessSubquery[T](LambdaSubqueryType subqueryType, Expression`1 expression)
at NHibernate.Criterion.Subqueries.WhereAll[T](Expression`1 expression)
at RBAC.Infrastructure.DataAccess.QueryObject.Implementation.UserQueryObject.FilterByUsersWereAllTheirFunctionRelationsAreScheduledForDeletion(IQueryOver`2 queryOver) in c:\APPL\dev\RBAC\Dev\RBAC.Infrastructure\DataAccess\QueryObject\Implementation\UserQueryObject.cs:line 157
at RBAC.Infrastructure.DataAccess.QueryObject.Implementation.UserQueryObject._ExecuteQuery(IQueryOver`2 queryOver) in c:\APPL\dev\RBAC\Dev\RBAC.Infrastructure\DataAccess\QueryObject\Implementation\UserQueryObject.cs:line 97
at RBAC.Infrastructure.DataAccess.QueryObject.Base.BaseQueryObject`1.ExecuteQuery(ISessionDecorater session) in c:\APPL\dev\RBAC\Dev\RBAC.Infrastructure\DataAccess\QueryObject\Base\BaseQueryObject.cs:line 21
at RBAC.Infrastructure.DataAccess.GenericDAO.Implementation.GenericDAO.GetByQueryObject[T](IQueryObject`1 queryObject) in c:\APPL\dev\RBAC\Dev\RBAC.Infrastructure\DataAccess\GenericDAO\Implementation\GenericDAO.cs:line 253
at RBAC.Infrastructure.BusinessService.ADModule.Implementation.DeletionFlagSetter.FlagUsersForDeletionWereAllRelationsAreFlaggedForDeletionForADRegisteredUsers() in c:\APPL\dev\RBAC\Dev\RBAC.Infrastructure\BusinessService\ADModule\Implementation\DeletionFlagSetter.cs:line 115
at RBAC.Infrastructure.BusinessService.ADModule.Implementation.ActiveDirectorySynchronizer.Synchronize() in c:\APPL\dev\RBAC\Dev\RBAC.Infrastructure\BusinessService\ADModule\Implementation\ActiveDirectorySynchronizer.cs:line 156

编辑 2:
如果我有一个用户列表,我认为 linq 中的等价物就是这个。

users.Where(x => x.UserHasFunctions.All(y => y.IsDeleted)).ToList();

最佳答案

尝试执行 QueryOver 查询时,我通常采取的第一步是提出正确的 SQL。根据您的 LINQ 查询,我认为它看起来像这样:

select
User.*
from
User
where
/* Or whatever "deleted" turns out to be, not necessarily '1' */
1 = all (select AccountStatus
from ContainerUser
where ContainerUser.UserId = User.Id);

基本上获取所有用户,其中该用户的所有 ContainerUser 行的 AccountStatus 均已删除。

所以在 QueryOver 中你可以这样写:

User userAlias = null;

session.QueryOver<User>(() => userAlias)
.WithSubquery.WhereValue(AccountStatus.DELETED).EqAll(QueryOver.Of<ContainerUser>()
.Where(uc => uc.User.Id == userAlias.Id)
.Select(uc => uc.AccountStatus));

// select list, more restrictions, etc.

关于c# - NHibernate QueryOver WithSubQuery WhereAll 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17337544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com