private ICriterion GetCollectionContainsCriteria(CollectionAccessExpression arg, Expression containsExpression) { EntityExpression rootEntity = EntityExpressionVisitor.FirstEntity(arg); var rootEntityType = rootEntity.Type; if (rootEntity.MetaData.HasProxy) { rootEntityType = rootEntity.MetaData.GetMappedClass(EntityMode.Poco); } DetachedCriteria query = DetachedCriteria.For(rootEntityType) .SetProjection(Projections.Id()); var visitor = new MemberNameVisitor(query.Adapt(session), true); visitor.Visit(arg); //TODO: this won't work for collections of values var containedEntity = QueryUtil.GetExpressionValue(containsExpression); var collectionIdPropertyName = visitor.MemberName + "." + arg.ElementExpression.MetaData.IdentifierPropertyName; var idValue = arg.ElementExpression.MetaData.GetIdentifier(containedEntity, EntityMode.Poco); query.Add(Restrictions.Eq(collectionIdPropertyName, idValue)); string identifierName = rootEntity.MetaData.IdentifierPropertyName; return(Subqueries.PropertyIn(identifierName, query)); }
private ICriterion GetExistsCriteria(MethodCallExpression expr) { EntityExpression rootEntity = EntityExpressionVisitor.FirstEntity(expr); string propertyName = MemberNameVisitor.GetMemberName(rootCriteria, expr); var rootEntityType = rootEntity.Type; if (rootEntity.MetaData.HasProxy) { rootEntityType = rootEntity.MetaData.GetMappedClass(EntityMode.Poco); } DetachedCriteria query = DetachedCriteria.For(rootEntityType) .SetProjection(Projections.Id()) .Add(Restrictions.IsNotEmpty(propertyName)); if (expr.Arguments.Count > 1) { var arg = (LambdaExpression)LinqUtil.StripQuotes(expr.Arguments[1]); string alias = arg.Parameters[0].Name; DetachedCriteria subquery = query.CreateCriteria(propertyName, alias); var temp = new WhereArgumentsVisitor(subquery.Adapt(session), session); temp.Visit(arg.Body); foreach (ICriterion c in temp.CurrentCriterions) { subquery.Add(c); } } string identifierName = rootEntity.GetAliasedIdentifierPropertyName(); return(Subqueries.PropertyIn(identifierName, query)); }