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)); }
protected override Expression VisitMethodCall(MethodCallExpression expr) { Type = BinaryCriterionType.Criteria; //TODO: don't hardcode this alias 'sub' Criteria = DetachedCriteria.ForEntityName(rootCriteria.GetEntityOrClassName(), "sub"); EntityExpression rootEntity = EntityExpressionVisitor.RootEntity(expr); if (rootEntity != null) { string identifierName = rootEntity.MetaData.IdentifierPropertyName; Criteria.Add(Restrictions.EqProperty(rootCriteria.Alias + "." + identifierName, "sub." + identifierName)); } var visitor = new RootVisitor(Criteria.Adapt(session), session, false); visitor.Visit(expr); return(expr); }
protected override Expression VisitMethodCall(MethodCallExpression expr) { Type = BinaryCriterionType.Criteria; //TODO: don't hardcode this alias 'sub' Criteria = DetachedCriteria.ForEntityName(rootCriteria.GetEntityOrClassName(), "sub"); EntityExpression rootEntity = EntityExpressionVisitor.RootEntity(expr); if (rootEntity != null) { string identifierName = rootEntity.MetaData.IdentifierPropertyName; Criteria.Add(Restrictions.EqProperty(rootCriteria.Alias + "." + identifierName, "sub." + identifierName)); } if (SelectArgumentsVisitor.SupportsMethod(expr.Method.Name)) { var projectionVisitor = new SelectArgumentsVisitor(Criteria.Adapt(session), session); projectionVisitor.Visit(expr); Criteria.SetProjection(projectionVisitor.Projection); } return expr; }
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)); }