internal ICriteria GenerateCriteria <Y>(ISearchQuery query, bool includeOrder, bool includePaging) { //build search criteria ICriteria criteria = NHibernateSession.CreateCriteria(typeof(Y)); //criteria IList <string> completedAliases = new List <string>(); if (query.HasCriteria) { criteria = ParseCriteria <Y>(query.GetCriteria()); IList <IQueryExpression> expressions = new List <IQueryExpression>(query.Expressions.Values); foreach (QueryExpression expression in expressions) { switch (expression.ExpressionType) { case ExpressionType.Equals: criteria.Add(Expression.Eq(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName), expression.Value)); break; case ExpressionType.EqualsProperty: criteria.Add(Expression.EqProperty(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName), expression.OtherPropertyName)); break; case ExpressionType.GreaterThan: criteria.Add(Expression.Gt(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName), expression.Value)); break; case ExpressionType.GreaterThanProperty: criteria.Add(Expression.GtProperty(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName), expression.OtherPropertyName)); break; case ExpressionType.GreaterThanOrEquals: criteria.Add(Expression.Ge(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName), expression.Value)); break; case ExpressionType.GreaterThanOrEqualsProperty: criteria.Add(Expression.GeProperty(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName), expression.OtherPropertyName)); break; case ExpressionType.LessThan: criteria.Add(Expression.Lt(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName), expression.Value)); break; case ExpressionType.LessThanProperty: criteria.Add(Expression.LtProperty(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName), expression.OtherPropertyName)); break; case ExpressionType.LessThanOrEquals: criteria.Add(Expression.Le(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName), expression.Value)); break; case ExpressionType.LessThanOrEqualsProperty: criteria.Add(Expression.LeProperty(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName), expression.OtherPropertyName)); break; case ExpressionType.NotEquals: criteria.Add(Expression.Not(Expression.Eq(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName), expression.Value))); break; case ExpressionType.NotEqualsProperty: criteria.Add(Expression.Not(Expression.EqProperty(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName), expression.OtherPropertyName))); break; case ExpressionType.Contains: criteria.Add(Expression.Like(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName), "%" + expression.Value + "%")); break; case ExpressionType.NotContaints: criteria.Add(Expression.Not(Expression.Like((this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName)), "%" + expression.Value + "%"))); break; case ExpressionType.IsNull: criteria.Add(Expression.IsNull(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName))); break; case ExpressionType.IsNotNull: criteria.Add(Expression.IsNotNull(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName))); break; case ExpressionType.In: criteria.Add(Expression.In(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName), expression.CollectionValues)); break; case ExpressionType.IsEmpty: criteria.Add(Expression.IsEmpty(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName))); break; case ExpressionType.IsNotEmpty: criteria.Add(Expression.IsNotEmpty(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName))); break; case ExpressionType.Between: criteria.Add(Expression.Between(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName), expression.CollectionValues[0], expression.CollectionValues[1])); break; case ExpressionType.SQL: criteria.Add(Expression.Sql(expression.QuerySQL)); break; default: criteria.Add(Expression.Like(this.CreateCriteriaAlias(completedAliases, criteria, expression.PropertyName), "%" + expression.Value + "%")); break; } } } criteria.SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer()); //order if (includeOrder && query.HasOrderClause) { foreach (ISortOrder order in query.OrderClauses) { if (order.IsAscending) { criteria.AddOrder(Order.Asc(this.CreateCriteriaAlias(completedAliases, criteria, order.PropertyName))); } else { criteria.AddOrder(Order.Desc(this.CreateCriteriaAlias(completedAliases, criteria, order.PropertyName))); } } } //paging if (includePaging && query.HasPaging) { criteria.SetMaxResults(query.GetMaxResults()); criteria.SetFirstResult(query.GetFirstResult()); } return(criteria); }
public BasePager <T> ToBasePager() { return(new BasePager <T>(_query.GetPage(), _query.GetMaxResults(), _items, TotalRows)); }