@@ -16,17 +16,21 @@ namespace AutoQueryable.Helpers
1616{
1717 public static class QueryBuilder
1818 {
19- public static IQueryable < dynamic > TotalCountQuery { get ; private set ; }
20-
21- public static dynamic Build < T > ( IClauseValueManager clauseValueManager , ICriteriaFilterManager criteriaFilterManager , IQueryable < T > query , ICollection < Criteria > criterias , IAutoQueryableProfile profile ) where T : class
19+ public static IQueryable < T > AddCriterias < T > ( IQueryable < T > query , ICollection < Criteria > criterias , ICriteriaFilterManager criteriaFilterManager ) where T : class
2220 {
2321 if ( criterias != null && criterias . Any ( ) )
2422 {
25- query = _addCriterias ( criteriaFilterManager , query , criterias ) ;
23+ return _addCriterias ( criteriaFilterManager , query , criterias ) ;
2624 }
25+
26+ return query ;
27+ }
28+
29+ public static dynamic Build < T > ( IClauseValueManager clauseValueManager , IQueryable < T > query , IAutoQueryableProfile profile ) where T : class
30+ {
31+ var totalCountQuery = query ;
2732 query = _addOrderBy ( query , clauseValueManager . OrderBy , profile ) ;
2833
29- TotalCountQuery = query ;
3034 if ( clauseValueManager . First )
3135 {
3236 return query . FirstOrDefault ( ) ;
@@ -55,7 +59,7 @@ public static dynamic Build<T>(IClauseValueManager clauseValueManager, ICriteria
5559 }
5660 }
5761
58- return queryProjection . HandleWrapping ( clauseValueManager ) ;
62+ return queryProjection . HandleWrapping ( clauseValueManager , totalCountQuery ) ;
5963 }
6064
6165 private static IQueryable < T > _handlePaging < T > ( IClauseValueManager clauseValueManager , IQueryable < T > query , IAutoQueryableProfile profile ) where T : class
@@ -96,7 +100,7 @@ private static IQueryable<T> _handlePaging<T>(IClauseValueManager clauseValueMan
96100 return query ;
97101 }
98102
99- public static dynamic HandleWrapping < TEntity > ( this IQueryable < TEntity > query , IClauseValueManager clauseValueManager ) where TEntity : class
103+ public static dynamic HandleWrapping < TEntity > ( this IQueryable < TEntity > query , IClauseValueManager clauseValueManager , IQueryable < TEntity > totalCountQuery ) where TEntity : class
100104 {
101105 if ( ! clauseValueManager . WrapWith . Any ( ) || clauseValueManager . First )
102106 {
@@ -114,7 +118,7 @@ public static dynamic HandleWrapping<TEntity>(this IQueryable<TEntity> query, IC
114118 }
115119 else if ( wrapperPart == WrapperAlias . TotalCount )
116120 {
117- wrapper . Add ( WrapperAlias . TotalCount , TotalCountQuery . Count ( ) ) ;
121+ wrapper . Add ( WrapperAlias . TotalCount , totalCountQuery . Count ( ) ) ;
118122 }
119123 }
120124
@@ -207,9 +211,37 @@ private static IQueryable<T> _addCriterias<T>(ICriteriaFilterManager criteriaFil
207211 Expression whereExpression = null ;
208212 foreach ( var c in criterias )
209213 {
210- var expression = BuildWhereExpression ( criteriaFilterManager , parentEntity , c , c . ColumnPath . ToArray ( ) ) ;
214+ if ( c . Criterias != null && c . Criterias . Count > 0 )
215+ {
216+ if ( c . Criterias . Count == 1 )
217+ {
218+ var expression = BuildWhereExpression ( criteriaFilterManager , parentEntity , c . Criterias . First ( ) , c . Criterias . First ( ) . ColumnPath . ToArray ( ) ) ;
219+ whereExpression = whereExpression == null ? expression : Expression . AndAlso ( whereExpression , expression ) ;
220+ }
211221
212- whereExpression = whereExpression == null ? expression : Expression . AndAlso ( whereExpression , expression ) ;
222+ List < Expression > expressions = new List < Expression > ( ) ;
223+ foreach ( var criteria in c . Criterias )
224+ {
225+ expressions . Add ( BuildWhereExpression ( criteriaFilterManager , parentEntity , criteria , criteria . ColumnPath . ToArray ( ) ) ) ;
226+ }
227+ Expression currentExpression = null ;
228+ foreach ( var expression in expressions )
229+ {
230+ if ( currentExpression == null )
231+ {
232+ currentExpression = expression ;
233+ continue ;
234+ }
235+ currentExpression = Expression . OrElse ( expression , currentExpression ) ;
236+ }
237+
238+ whereExpression = whereExpression == null ? currentExpression : Expression . AndAlso ( whereExpression , currentExpression ) ; // TODO
239+ }
240+ else
241+ {
242+ var expression = BuildWhereExpression ( criteriaFilterManager , parentEntity , c , c . ColumnPath . ToArray ( ) ) ;
243+ whereExpression = whereExpression == null ? expression : Expression . AndAlso ( whereExpression , expression ) ;
244+ }
213245 }
214246
215247 return source . Where ( Expression . Lambda < Func < T , bool > > ( whereExpression , parentEntity ) ) ;
@@ -237,15 +269,29 @@ private static IQueryable<T> _addOrderBy<T>(IQueryable<T> source, Dictionary<str
237269 {
238270 PropertyName = v . Name
239271 } ) ;
272+ var i = 0 ;
240273 foreach ( var column in columns )
241274 {
242275 var desc = orderClause . FirstOrDefault ( o => string . Equals ( o . Key , column . PropertyName , StringComparison . OrdinalIgnoreCase ) ) . Value ;
276+ string method ;
243277 if ( desc ) {
244- source = source . Call ( QueryableMethods . OrderByDescending , column . PropertyName ) ;
245- } else
278+ method = QueryableMethods . OrderByDescending ;
279+ if ( i > 0 )
280+ {
281+ method = QueryableMethods . ThenByDescending ;
282+ }
283+ }
284+ else
246285 {
247- source = source . Call ( QueryableMethods . OrderBy , column . PropertyName ) ;
286+ method = QueryableMethods . OrderBy ;
287+ if ( i > 0 )
288+ {
289+ method = QueryableMethods . ThenBy ;
290+ }
248291 }
292+
293+ source = source . Call ( method , column . PropertyName ) ;
294+ i ++ ;
249295 }
250296 return source ;
251297 }
0 commit comments