protected override Expression VisitMethodCall(MethodCallExpression expr)
        {
            if (WhereArgumentsVisitor.SupportsMethod(expr.Method.Name))
            {
                return(VisitCriterionExpression(expr));
            }

            //TODO: this needs to be refactored...
            //create any collection subcriteria and get the collection access expression
            MemberNameVisitor memberVisitor = new MemberNameVisitor(_rootCriteria, true);

            memberVisitor.Visit(expr.Arguments[0]);
            CollectionAccessExpression collectionExpr = (CollectionAccessExpression)memberVisitor.CurrentExpression;

            string             propertyName = null;
            IProjection        projection   = null;
            PropertyProjection currentProjection;

            if (expr.Arguments.Count > 1)
            {
                propertyName = MemberNameVisitor.GetMemberName(_rootCriteria, expr.Arguments[1]);
            }
            else if ((currentProjection = _rootCriteria.GetProjection() as PropertyProjection) != null)
            {
                propertyName = currentProjection.PropertyName;
            }

            switch (expr.Method.Name)
            {
            case "Average":
                projection = NHProjections.Avg(propertyName);
                break;

            case "Count":
            case "LongCount":
                if (expr.Arguments.Count > 1)
                {
                    _rootCriteria.Add(WhereArgumentsVisitor.GetCriterion(_rootCriteria, _session, expr.Arguments[1]));
                }

                if (collectionExpr != null)
                {
                    //get count on collection element's identifier property
                    propertyName = memberVisitor.MemberName + "." + collectionExpr.ElementExpression.MetaData.IdentifierPropertyName;
                    projection   = NHProjections.Count(propertyName);
                }
                else
                {
                    projection = NHProjections.RowCount();
                }
                break;

            case "Max":
                projection = NHProjections.Max(propertyName);
                break;

            case "Min":
                projection = NHProjections.Min(propertyName);
                break;

            case "Sum":
                projection = NHProjections.Sum(propertyName);
                break;

            default:
                throw new NotImplementedException("The method '" + expr.Method.Name + "' is not implemented.");
            }

            _projections.Add(projection);
            return(expr);
        }
Example #2
0
 public CountProjection Count()
 {
     return(Projections.Count(PropertyName));
 }
Example #3
0
 /// <summary>
 /// A property value count
 /// </summary>
 public static CountProjection Count(Expression <Func <object> > expression)
 {
     return(Projections.Count(ExpressionProcessor.FindMemberExpression(expression.Body)));
 }