扩展类
代码语言:javascript复制 1 public static class LinqExtensions
2 {
3 /// <summary>
4 /// 创建lambda表达式:p=>true
5 /// </summary>
6 /// <typeparam name="T">对象名称(类名)</typeparam>
7 /// <returns></returns>
8 public static Expression<Func<T, bool>> True<T>()
9 {
10 return p => true;
11 }
12
13 /// <summary>
14 /// 创建lambda表达式:p=>false
15 /// </summary>
16 /// <typeparam name="T">对象名称(类名)</typeparam>
17 /// <returns></returns>
18 public static Expression<Func<T, bool>> False<T>()
19 {
20 return p => false;
21 }
22
23 /// <summary>
24 /// 创建lambda表达式:p=>p.propertyName
25 /// </summary>
26 /// <typeparam name="T">对象名称(类名)</typeparam>
27 /// <typeparam name="TKey">参数类型</typeparam>
28 /// <param name="propertyName">字段名称(数据库中字段名称)</param>
29 /// <returns></returns>
30 public static Expression<Func<T, TKey>> GetOrderExpression<T, TKey>(string propertyName)
31 {
32 ParameterExpression parameter = Expression.Parameter(typeof(T), "p");
33 return Expression.Lambda<Func<T, TKey>>(Expression.Property(parameter, propertyName), parameter);
34 }
35
36 /// <summary>
37 /// 创建lambda表达式:p=>p.propertyName == propertyValue
38 /// </summary>
39 /// <typeparam name="T">对象名称(类名)</typeparam>
40 /// <param name="propertyName">字段名称(数据库中字段名称)</param>
41 /// <param name="propertyValue">数据值</param>
42 /// <returns></returns>
43 public static Expression<Func<T, bool>> CreateEqual<T>(string propertyName, object propertyValue, Type typeValue)
44 {
45 ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
46 MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
47 ConstantExpression constant = Expression.Constant(propertyValue, typeValue);//创建常数
48 return Expression.Lambda<Func<T, bool>>(Expression.Equal(member, constant), parameter);
49 }
50
51 /// <summary>
52 /// 创建lambda表达式:p=>p.propertyName != propertyValue
53 /// </summary>
54 /// <typeparam name="T">对象名称(类名)</typeparam>
55 /// <param name="propertyName">字段名称(数据库中字段名称)</param>
56 /// <param name="propertyValue">数据值</param>
57 /// <returns></returns>
58 public static Expression<Func<T, bool>> CreateNotEqual<T>(string propertyName, object propertyValue, Type typeValue)
59 {
60 ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
61 MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
62 ConstantExpression constant = Expression.Constant(propertyValue, typeValue);//创建常数
63 return Expression.Lambda<Func<T, bool>>(Expression.NotEqual(member, constant), parameter);
64 }
65
66 /// <summary>
67 /// 创建lambda表达式:p=>p.propertyName > propertyValue
68 /// </summary>
69 /// <typeparam name="T">对象名称(类名)</typeparam>
70 /// <param name="propertyName">字段名称(数据库中字段名称)</param>
71 /// <param name="propertyValue">数据值</param>
72 /// <returns></returns>
73 public static Expression<Func<T, bool>> CreateGreaterThan<T>(string propertyName, object propertyValue, Type typeValue)
74 {
75 ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
76 MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
77 ConstantExpression constant = Expression.Constant(propertyValue, typeValue);//创建常数
78 return Expression.Lambda<Func<T, bool>>(Expression.GreaterThan(member, constant), parameter);
79 }
80
81 /// <summary>
82 /// 创建lambda表达式:p=>p.propertyName小于propertyValue
83 /// </summary>
84 /// <typeparam name="T">对象名称(类名)</typeparam>
85 /// <param name="propertyName">字段名称(数据库中字段名称)</param>
86 /// <param name="propertyValue">数据值</param>
87 /// <returns></returns>
88 public static Expression<Func<T, bool>> CreateLessThan<T>(string propertyName, object propertyValue, Type typeValue)
89 {
90 ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
91 MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
92 ConstantExpression constant = Expression.Constant(propertyValue, typeValue);//创建常数
93 return Expression.Lambda<Func<T, bool>>(Expression.LessThan(member, constant), parameter);
94 }
95
96 /// <summary>
97 /// 创建lambda表达式:p=>p.propertyName >= propertyValue
98 /// </summary>
99 /// <typeparam name="T">对象名称(类名)</typeparam>
100 /// <param name="propertyName">字段名称(数据库中字段名称)</param>
101 /// <param name="propertyValue">数据值</param>
102 /// <returns></returns>
103 public static Expression<Func<T, bool>> CreateGreaterThanOrEqual<T>(string propertyName, object propertyValue, Type typeValue)
104 {
105 ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
106 MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
107 ConstantExpression constant = Expression.Constant(propertyValue, typeValue);//创建常数
108 return Expression.Lambda<Func<T, bool>>(Expression.GreaterThanOrEqual(member, constant), parameter);
109 }
110
111 /// <summary>
112 /// 创建lambda表达式:p=> p.propertyName 小于= propertyValue
113 /// </summary>
114 /// <typeparam name="T">对象名称(类名)</typeparam>
115 /// <param name="propertyName">字段名称(数据库中字段名称)</param>
116 /// <param name="propertyValue">数据值</param>
117 /// <returns></returns>
118 public static Expression<Func<T, bool>> CreateLessThanOrEqual<T>(string propertyName, object propertyValue, Type typeValue)
119 {
120 ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
121 MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
122 ConstantExpression constant = Expression.Constant(propertyValue, typeValue);//创建常数
123 return Expression.Lambda<Func<T, bool>>(Expression.LessThanOrEqual(member, constant), parameter);
124 }
125
126 /// <summary>
127 /// 创建lambda表达式:p=>p.propertyName.Contains(propertyValue)
128 /// </summary>
129 /// <typeparam name="T">对象名称(类名)</typeparam>
130 /// <param name="propertyName">字段名称(数据库中字段名称)</param>
131 /// <param name="propertyValue">数据值</param>
132 /// <returns></returns>
133 public static Expression<Func<T, bool>> GetContains<T>(string propertyName, string propertyValue)
134 {
135 ParameterExpression parameter = Expression.Parameter(typeof(T), "p");
136 MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
137 MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
138 ConstantExpression constant = Expression.Constant(propertyValue, typeof(string));
139 return Expression.Lambda<Func<T, bool>>(Expression.Call(member, method, constant), parameter);
140 }
141
142 /// <summary>
143 /// 创建lambda表达式:!(p=>p.propertyName.Contains(propertyValue))
144 /// </summary>
145 /// <typeparam name="T">对象名称(类名)</typeparam>
146 /// <param name="propertyName">字段名称(数据库中字段名称)</param>
147 /// <param name="propertyValue">数据值</param>
148 /// <returns></returns>
149 public static Expression<Func<T, bool>> GetNotContains<T>(string propertyName, string propertyValue)
150 {
151 ParameterExpression parameter = Expression.Parameter(typeof(T), "p");
152 MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
153 MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
154 ConstantExpression constant = Expression.Constant(propertyValue, typeof(string));
155 return Expression.Lambda<Func<T, bool>>(Expression.Not(Expression.Call(member, method, constant)), parameter);
156 }
157
158 /// <summary>
159 /// 功能描述:拼接Or
160 /// 作 者:beck.huang
161 /// 创建日期:2018-11-30 15:35:10
162 /// 任务编号:好餐谋后台管理系统
163 /// </summary>
164 /// <param name="expression1">expression1</param>
165 /// <param name="expression2">expression2</param>
166 /// <returns>返回值</returns>
167 public static Expression<Func<T, bool>> Or<T>(Expression<Func<T, bool>> expression1, Expression<Func<T, bool>> expression2)
168 {
169 return Compose(expression1, expression2, Expression.OrElse);
170 }
171
172 /// <summary>
173 /// 功能描述:拼接And
174 /// 作 者:beck.huang
175 /// 创建日期:2018-11-30 15:35:18
176 /// 任务编号:好餐谋后台管理系统
177 /// </summary>
178 /// <param name="expression1">expression1</param>
179 /// <param name="expression2">expression2</param>
180 /// <returns>返回值</returns>
181 public static Expression<Func<T, bool>> And<T>(Expression<Func<T, bool>> expression1, Expression<Func<T, bool>> expression2)
182 {
183 return Compose(expression1, expression2, Expression.AndAlso);
184 }
185
186 /// <summary>
187 /// 功能描述:合并2个表达式
188 /// 作 者:beck.huang
189 /// 创建日期:2018-11-30 15:35:26
190 /// 任务编号:好餐谋后台管理系统
191 /// </summary>
192 /// <param name="first">first</param>
193 /// <param name="second">second</param>
194 /// <param name="merge">merge</param>
195 /// <returns>返回值</returns>
196 public static Expression<T> Compose<T>(Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
197 {
198 var map = first.Parameters
199 .Select((f, i) => new { f, s = second.Parameters[i] })
200 .ToDictionary(p => p.s, p => p.f);
201 var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
202 return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
203 }
204 private class ParameterRebinder : ExpressionVisitor
205 {
206 readonly Dictionary<ParameterExpression, ParameterExpression> map;
207 /// <summary>
208 /// Initializes a new instance of the <see cref="ParameterRebinder"/> class.
209 /// </summary>
210 /// <param name="map">The map.</param>
211 ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
212 {
213 this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
214 }
215 /// <summary>
216 /// Replaces the parameters.
217 /// </summary>
218 /// <param name="map">The map.</param>
219 /// <param name="exp">The exp.</param>
220 /// <returns>Expression</returns>
221 public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
222 {
223 return new ParameterRebinder(map).Visit(exp);
224 }
225 protected override Expression VisitParameter(ParameterExpression p)
226 {
227 ParameterExpression replacement;
228
229 if (map.TryGetValue(p, out replacement))
230 {
231 p = replacement;
232 }
233 return base.VisitParameter(p);
234 }
235 }
236 }