forked from dotnetcore/Util
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.Queryable.cs
More file actions
163 lines (154 loc) · 8.44 KB
/
Extensions.Queryable.cs
File metadata and controls
163 lines (154 loc) · 8.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using Util.Datas.Queries;
using Util.Datas.Queries.Criterias;
using Util.Datas.Queries.Internal;
using Util.Domains.Repositories;
namespace Util {
/// <summary>
/// 查询扩展
/// </summary>
public static partial class Extensions {
/// <summary>
/// 添加查询条件
/// </summary>
/// <typeparam name="TEntity">实体类型</typeparam>
/// <param name="source">数据源</param>
/// <param name="criteria">查询条件对象</param>
public static IQueryable<TEntity> Where<TEntity>( this IQueryable<TEntity> source, ICriteria<TEntity> criteria ) where TEntity : class {
if( source == null )
throw new ArgumentNullException( nameof( source ) );
if( criteria == null )
throw new ArgumentNullException( nameof( criteria ) );
var predicate = criteria.GetPredicate();
if( predicate == null )
return source;
return source.Where( predicate );
}
/// <summary>
/// 添加查询条件
/// </summary>
/// <typeparam name="TEntity">实体类型</typeparam>
/// <param name="source">数据源</param>
/// <param name="predicate">查询条件</param>
/// <param name="condition">该值为true时添加查询条件,否则忽略</param>
public static IQueryable<TEntity> WhereIf<TEntity>( this IQueryable<TEntity> source, Expression<Func<TEntity, bool>> predicate, bool condition ) where TEntity : class {
if( source == null )
throw new ArgumentNullException( nameof( source ) );
if( condition == false )
return source;
return source.Where( predicate );
}
/// <summary>
/// 添加查询条件
/// </summary>
/// <typeparam name="TEntity">实体类型</typeparam>
/// <param name="source">数据源</param>
/// <param name="predicate">查询条件,如果参数值为空,则忽略该查询条件,范例:t => t.Name == "",该查询条件被忽略。
/// 注意:一次仅能添加一个条件,范例:t => t.Name == "a" && t.Mobile == "123",不支持,将抛出异常</param>
public static IQueryable<TEntity> WhereIfNotEmpty<TEntity>( this IQueryable<TEntity> source, Expression<Func<TEntity, bool>> predicate ) where TEntity : class {
if( source == null )
throw new ArgumentNullException( nameof( source ) );
predicate = Helper.GetWhereIfNotEmptyExpression( predicate );
if( predicate == null )
return source;
return source.Where( predicate );
}
/// <summary>
/// 添加范围查询条件
/// </summary>
/// <typeparam name="TEntity">实体类型</typeparam>
/// <param name="source">数据源</param>
/// <typeparam name="TProperty">属性类型</typeparam>
/// <param name="propertyExpression">属性表达式,范例:t => t.Age</param>
/// <param name="min">最小值</param>
/// <param name="max">最大值</param>
/// <param name="boundary">包含边界</param>
public static IQueryable<TEntity> Between<TEntity, TProperty>( this IQueryable<TEntity> source, Expression<Func<TEntity, TProperty>> propertyExpression, int? min, int? max, Boundary boundary = Boundary.Both ) where TEntity : class {
if( source == null )
throw new ArgumentNullException( nameof( source ) );
return source.Where( new IntSegmentCriteria<TEntity, TProperty>( propertyExpression, min, max, boundary ) );
}
/// <summary>
/// 添加范围查询条件
/// </summary>
/// <typeparam name="TEntity">实体类型</typeparam>
/// <param name="source">数据源</param>
/// <typeparam name="TProperty">属性类型</typeparam>
/// <param name="propertyExpression">属性表达式,范例:t => t.Age</param>
/// <param name="min">最小值</param>
/// <param name="max">最大值</param>
/// <param name="boundary">包含边界</param>
public static IQueryable<TEntity> Between<TEntity, TProperty>( this IQueryable<TEntity> source, Expression<Func<TEntity, TProperty>> propertyExpression, double? min, double? max, Boundary boundary = Boundary.Both ) where TEntity : class {
if( source == null )
throw new ArgumentNullException( nameof( source ) );
return source.Where( new DoubleSegmentCriteria<TEntity, TProperty>( propertyExpression, min, max, boundary ) );
}
/// <summary>
/// 添加范围查询条件
/// </summary>
/// <typeparam name="TEntity">实体类型</typeparam>
/// <param name="source">数据源</param>
/// <typeparam name="TProperty">属性类型</typeparam>
/// <param name="propertyExpression">属性表达式,范例:t => t.Age</param>
/// <param name="min">最小值</param>
/// <param name="max">最大值</param>
/// <param name="boundary">包含边界</param>
public static IQueryable<TEntity> Between<TEntity, TProperty>( this IQueryable<TEntity> source, Expression<Func<TEntity, TProperty>> propertyExpression, decimal? min, decimal? max, Boundary boundary = Boundary.Both ) where TEntity : class {
if( source == null )
throw new ArgumentNullException( nameof( source ) );
return source.Where( new DecimalSegmentCriteria<TEntity, TProperty>( propertyExpression, min, max, boundary ) );
}
/// <summary>
/// 添加范围查询条件
/// </summary>
/// <typeparam name="TEntity">实体类型</typeparam>
/// <param name="source">数据源</param>
/// <typeparam name="TProperty">属性类型</typeparam>
/// <param name="propertyExpression">属性表达式,范例:t => t.Time</param>
/// <param name="min">最小值</param>
/// <param name="max">最大值</param>
/// <param name="includeTime">是否包含时间</param>
/// <param name="boundary">包含边界</param>
public static IQueryable<TEntity> Between<TEntity, TProperty>( this IQueryable<TEntity> source, Expression<Func<TEntity, TProperty>> propertyExpression, DateTime? min, DateTime? max, bool includeTime = true, Boundary? boundary = null ) where TEntity : class {
if( source == null )
throw new ArgumentNullException( nameof( source ) );
if( includeTime )
return source.Where( new DateTimeSegmentCriteria<TEntity, TProperty>( propertyExpression, min, max, boundary ?? Boundary.Both ) );
return source.Where( new DateSegmentCriteria<TEntity, TProperty>( propertyExpression, min, max, boundary ?? Boundary.Left ) );
}
/// <summary>
/// 分页,包含排序
/// </summary>
/// <typeparam name="TEntity">实体类型</typeparam>
/// <param name="source">数据源</param>
/// <param name="pager">分页对象</param>
public static IQueryable<TEntity> Page<TEntity>( this IQueryable<TEntity> source, IPager pager ) {
if( source == null )
throw new ArgumentNullException( nameof( source ) );
if( pager == null )
throw new ArgumentNullException( nameof( pager ) );
if( string.IsNullOrWhiteSpace( pager.Order ) )
pager.Order = "Id";
if( pager.TotalCount <= 0 )
pager.TotalCount = source.Count();
return source.OrderBy( pager.Order ).Skip( pager.GetSkipCount() ).Take( pager.PageSize );
}
/// <summary>
/// 转换为分页列表,包含排序分页操作
/// </summary>
/// <typeparam name="TEntity">实体类型</typeparam>
/// <param name="source">数据源</param>
/// <param name="pager">分页对象</param>
public static PagerList<TEntity> ToPagerList<TEntity>( this IQueryable<TEntity> source, IPager pager ) {
if( source == null )
throw new ArgumentNullException( nameof( source ) );
if( pager == null )
throw new ArgumentNullException( nameof( pager ) );
return new PagerList<TEntity>( pager, source.Page( pager ).ToList() );
}
}
}