public AggregateProjection Max() { return(Projections.Max(PropertyName)); }
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); }
public IList <TaskPrimaryDataDTO> GetTasks(int pageNo, int pageSize, TaskSearchCriteria criteria) { using (var session = Hibernate.SessionFactory.OpenSession()) { Task t = null; TaskStatus s = null; TaskPriority p = null; TaskGenre g = null; TaskComment c = null; TaskComment c_sub = null; User uc = null; var query = session.QueryOver(() => t) .JoinAlias(() => t.Status, () => s) .JoinAlias(() => t.Priority, () => p) .JoinAlias(() => t.Genre, () => g) .Left.JoinAlias(() => t.Comments, () => c) .Left.JoinAlias(() => c.User, () => uc) .Where( Res.EqProperty( Pro.Conditional(Res.IsNull("c.Id"), Pro.Constant(0), Pro.Property("c.Id")), Pro.SubQuery(QueryOver.Of(() => c_sub) .Where(() => c_sub.Task.Id == c.Task.Id) .Select(Pro.Conditional(Res.IsNull("c_sub.Id"), Pro.Constant(0), Pro.Max("c_sub.Id")))) )); if (criteria.TaskAuthorUsername.IsNotNull()) { query.Where(() => t.Author == criteria.TaskAuthorUsername); } if (criteria.ShowTasksOnly) { query.Where(() => g.Id == 4); } if (criteria.ShowInstallationsOnly) { query.Where(() => g.Id == 2); } if (criteria.ShowTonersOnly) { query.Where(() => g.Id == 3); } if (criteria.ShowUpdatesOnly) { query.Where(() => g.Id == 1); } if (criteria.ShowWithoutCanceledTasks) { query.Where(Res.Not(Res.Eq(Pro.Property("s.Id"), 1))); } if (criteria.TaskId.IsNotNull()) { query.Where(() => t.Id == criteria.TaskId.Value); } if (criteria.Topic.IsNotNull()) { query.Where(Res.InsensitiveLike(Pro.Property("t.Topic"), criteria.Topic, MatchMode.Anywhere)); } if (criteria.Content.IsNotNull()) { query.Where(Res.InsensitiveLike(Pro.Property("t.Content"), criteria.Content, MatchMode.Anywhere)); } if (criteria.StartDate.IsNotNull()) { query.Where(Res.Eq(Pro.Cast(SqlType.Date, Pro.Property("t.StartPeriod")), criteria.StartDate.Value)); } if (criteria.StartHour.IsNotNull()) { query.Where(Res.Eq(Pro.Cast(SqlType.Time, Pro.Property("t.StartPeriod")), criteria.StartHour.Value.ToString())); } if (criteria.EndDate.IsNotNull()) { query.Where(Res.Eq(Pro.Cast(SqlType.Date, Pro.Property("t.EndPeriod")), criteria.EndDate.Value)); } if (criteria.EndDate.IsNotNull()) { query.Where(Res.Eq(Pro.Cast(SqlType.Time, Pro.Property("t.EndPeriod")), criteria.EndHour.Value.ToString())); } if (criteria.TaskPriorityId.IsNotNull()) { query.Where(() => p.Id == criteria.TaskPriorityId); } if (criteria.TaskStatusId.IsNotNull()) { query.Where(() => s.Id == criteria.TaskStatusId); } if (criteria.TaskParticipantId.IsNotNull()) { Task subT = null; User subU = null; var subquery = QueryOver.Of(() => subT) .JoinAlias(() => subT.Users, () => subU) .Where(Res.And( Res.EqProperty(Pro.Property("t.Id"), Pro.Property("subT.Id")), Res.Eq(Pro.Property("subU.Id"), criteria.TaskParticipantId))) .SelectList(l => l .Select(() => subU.Id)); query.WithSubquery.WhereExists(subquery); } if (criteria.Comment.IsNotNull()) { TaskComment subC = null; Task subT = null; var subquery = QueryOver.Of(() => subC) .JoinAlias(() => subC.Task, () => subT) .Where(Res.And( Res.EqProperty(Pro.Property("subT.Id"), Pro.Property("t.Id")), Res.InsensitiveLike(Pro.Property("subC.Content"), criteria.Comment, MatchMode.Anywhere))) .SelectList(l => l .Select(() => subT.Id)); query.WithSubquery.WhereExists(subquery); } if (criteria.CommentAuthorId.IsNotNull()) { TaskComment subC = null; Task subT = null; User subU = null; var subquery = QueryOver.Of(() => subC) .JoinAlias(() => subC.Task, () => subT) .JoinAlias(() => subC.User, () => subU) .Where(Res.And( Res.EqProperty(Pro.Property("subT.Id"), Pro.Property("t.Id")), Res.Eq(Pro.Property("subU.Id"), criteria.CommentAuthorId))) .SelectList(l => l .Select(() => subT.Id)); query.WithSubquery.WhereExists(subquery); } query.SelectList(l => l .Select(() => t.Id) .Select(() => t.Topic) .Select(() => t.Content) .Select(() => t.StartPeriod) .Select(() => t.EndPeriod) .Select(() => c.Id) .Select(() => c.Content) .Select(() => c.Date) .Select(() => uc.Username) .Select(() => s.Name) .Select(() => p.Name) .Select(() => g.Name) .Select(() => t.Author)) .OrderBy(() => t.Id).Desc .TransformUsing(Transformers.AliasToBeanConstructor(typeof(TaskPrimaryDataDTO).GetConstructors()[1])) .Skip(pageSize * (pageNo - 1)).Take(pageSize); return(query.List <TaskPrimaryDataDTO>()); #region Above generates sql like... /* * SELECT this_.Id AS y0_, * this_.Topic AS y1_, * this_.StartPeriod AS y2_, * this_.EndPeriod AS y3_, * c4_.Id AS y4_, * c4_.Content AS y5_, * c4_.Date AS y6_, * u5_.Username AS y7_, * s1_.Name AS y8_, * p2_.Name AS y9_, * g3_.Name AS y10_ * FROM `Task` this_ * LEFT OUTER JOIN `TaskComment` c4_ ON this_.Id=c4_.Task_id * LEFT OUTER JOIN `User` u5_ ON c4_.User_id=u5_.Id * INNER JOIN `TaskGenre` g3_ ON this_.Genre_id=g3_.Id * INNER JOIN `TaskStatus` s1_ ON this_.Status_id=s1_.Id * INNER JOIN `TaskPriority` p2_ ON this_.Priority_id=p2_.Id * WHERE (CASE WHEN c4_.Id IS NULL THEN ?p0 ELSE c4_.Id END) = * (SELECT (CASE WHEN this_0_.Id IS NULL THEN ?p1 ELSE max(this_0_.Id) END) AS y0_ FROM `TaskComment` this_0_) * ORDER BY this_.Id DESC * LIMIT ?p2; * * ?p0 = 0 [Type: Int32 (0)], * ?p1 = 0 [Type: Int32 (0)], * ?p2 = 50 [Type: Int32 (0)] */ #endregion } }
/// <summary> /// A property maximum value /// </summary> public static AggregateProjection Max(Expression <Func <object> > expression) { return(Projections.Max(ExpressionProcessor.FindMemberExpression(expression.Body))); }