EF 拉姆达 linq 帮助类

(这个类是很早以前在网上找的,忘记出处请原谅。)

一、基本用法

[Route("List")]
public ApiResult GetList(int page, int limit, string sort = null, string order = null, string filters = null, string query = null)
{
    PageModel p = new PageModel();
    if (filters != null)
    {
        p.filters = Newtonsoft.Json.JsonConvert.DeserializeObject<Filter[]>(filters);
    }
 
    p.page = page;
    p.rows = limit;
    p.sort = sort;
    p.order = order;
 
    if (p.page <= 0)
    {
        p.page = 1;
    }
    if (p.rows <= 0)
    {
        p.rows = 10;
    }
 
    var data = manage.GetQueryable().Select(d => d);
    //过滤
    data = data.Where(p.filters);
    //搜索条件
    if (query != null && query.Length > 0)
    {
        data = data.Where(new string[] { "UserName", "RealName" }, query);
    }
    //排序
    if (order != "normal" && sort != null)
    {
        bool isAsc = order == "asc";
        data = data.OrderBy(new[] { sort }, new[] { isAsc });
    }
    else
    {
        //默认排序
        data = data.OrderBy(d => d.UserID);
    }
 
    DataModel pageData = new DataModel();
    pageData.page = p.page;
    pageData.total = data.Count();
    pageData.rows = data.Skip((p.page - 1) * p.rows).Take(p.rows).ToList();
 
    ApiResult result = new ApiResult();
    result.success = true;
    result.data = pageData;
    return result;
}
View Code

完整案例:结合前端 elementUI 组件 可以实现分页 查询、排序、过滤等。

https://www.cnblogs.com/hao-1234-1234/p/9647322.html#4225885

二、linq 帮助类

 重写了where和orderby方法。 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Linq.Expressions;
  5 using System.Reflection;
  6 using System.Web;
  7 using Vegetable.Models;
  8 
  9 namespace Vegetable.DAL
 10 {
 11     public static class QueryableExtension
 12     {
 13 
 14         public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string[] propertyName, bool[] ascending) where T : class
 15         {
 16             Type type = typeof(T);
 17 
 18             for (int i = 0; i < propertyName.Length; i++)
 19             {
 20 
 21                 PropertyInfo property = type.GetProperty(propertyName[i]);
 22                 if (property == null)
 23                     throw new ArgumentException("propertyName", "Not Exist");
 24 
 25                 ParameterExpression param = Expression.Parameter(type, "p");
 26                 Expression propertyAccessExpression = Expression.MakeMemberAccess(param, property);
 27                 LambdaExpression orderByExpression = Expression.Lambda(propertyAccessExpression, param);
 28 
 29                 string methodName = ascending[i] ? "OrderBy" : "OrderByDescending";
 30                 if (i != 0)
 31                 {
 32                     methodName = ascending[i] ? "ThenBy" : "ThenByDescending";
 33                 }
 34 
 35                 MethodCallExpression resultExp = Expression.Call(typeof(Queryable), methodName, new Type[] { type, property.PropertyType }, source.Expression, Expression.Quote(orderByExpression));
 36                 source = source.Provider.CreateQuery<T>(resultExp);
 37 
 38             }
 39 
 40 
 41             return source;
 42         }
 43 
 44 
 45         public static IQueryable<T> Where<T>(this IQueryable<T> source, FilterRule[] filterRules) where T : class
 46         {
 47             if (filterRules == null)
 48             {
 49                 return source;
 50             }
 51             Type type = typeof(T);
 52 
 53             ParameterExpression param = Expression.Parameter(type, "c");
 54 
 55             Expression<Func<T, bool>> op = null;
 56 
 57             foreach (var rule in filterRules)
 58             {
 59                 PropertyInfo property = type.GetProperty(rule.Field);
 60                 if (property == null)
 61                 {
 62                     continue;
 63                 }
 64                 //c.Field==Value
 65                 //c=>c.Field.Contains(Value)
 66                 Expression left = Expression.Property(param, property);
 67 
 68                 Expression right = Expression.Constant(rule.Value);
 69                 Type valueType = property.PropertyType;
 70                 if (rule.Value == null || rule.Value == "") continue;
 71                 DateTime inputDateTime = DateTime.Now;
 72                 try
 73                 {
 74 
 75                     if (valueType == typeof(int) || valueType == typeof(int?))
 76                     {
 77                         right = Expression.Constant(Convert.ToInt32(rule.Value.Split('.')[0]));
 78                     }
 79                     else if (valueType == typeof(short) || valueType == typeof(short?))
 80                     {
 81                         right = Expression.Constant(Convert.ToInt16(rule.Value.Split('.')[0]));
 82                     }
 83                     else if (valueType == typeof(byte) || valueType == typeof(byte?))
 84                     {
 85                         right = Expression.Constant(Convert.ToByte(rule.Value.Split('.')[0]));
 86                     }
 87                     else if (valueType == typeof(long) || valueType == typeof(long?))
 88                     {
 89                         right = Expression.Constant(Convert.ToInt64(rule.Value));
 90                     }
 91                     else if (valueType == typeof(float) || valueType == typeof(float?))
 92                     {
 93                         right = Expression.Constant(Convert.ToSingle(rule.Value));
 94                     }
 95                     else if (valueType == typeof(double) || valueType == typeof(double?))
 96                     {
 97                         right = Expression.Constant(Convert.ToDouble(rule.Value));
 98                     }
 99                     else if (valueType == typeof(decimal) || valueType == typeof(decimal?))
100                     {
101                         right = Expression.Constant(Convert.ToDecimal(rule.Value));
102                     }
103                     else if (valueType == typeof(DateTime) || valueType == typeof(DateTime?))
104                     {
105                         inputDateTime = Convert.ToDateTime(rule.Value);
106                         right = Expression.Constant(Convert.ToDateTime(rule.Value));
107                     }
108                     else if (valueType == typeof(Guid) || valueType == typeof(Guid?))
109                     {
110                         right = Expression.Constant(Guid.Parse(rule.Value));
111                     }
112 
113                 }
114                 catch (Exception ex)
115                 {
116                     Console.WriteLine(ex.Message);
117                     break;
118                 }
119 
120 
121                 Expression filter = Expression.Equal(left, right);
122                 Expression filter2 = null;
123                 MethodInfo method;
124 
125                 switch (rule.Op)
126                 {
127                     case OP.contains:
128                         //BinaryExpression
129                         if (valueType == typeof(string))
130                         {
131                             method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
132                             filter = Expression.Call(left, method, right);
133                         }
134                         else if (valueType == typeof(DateTime) || valueType == typeof(DateTime?))
135                         {
136                             right = Expression.Constant(inputDateTime.Date);
137                             filter = Expression.GreaterThanOrEqual(left, right);
138                             right = Expression.Constant(inputDateTime.Date.AddDays(1));
139                             filter2 = Expression.LessThan(left, right);
140                         }
141                         else
142                         {
143                             filter = Expression.Equal(left, right);
144                         }
145                         break;
146                     case OP.equal:
147                         filter = Expression.Equal(left, right);
148                         break;
149                     case OP.notequal:
150                         filter = Expression.NotEqual(left, right);
151                         break;
152                     case OP.beginwith:
153                         method = typeof(string).GetMethod("StartsWith", new[] { typeof(string) });
154 
155                         filter = Expression.Call(left, method, right);
156                         break;
157                     case OP.endwith:
158                         method = typeof(string).GetMethod("EndsWith", new[] { typeof(string) });
159 
160                         filter = Expression.Call(left, method, right);
161                         break;
162                     case OP.less:
163                         filter = Expression.LessThan(left, right);
164                         break;
165                     case OP.lessorequal:
166                         filter = Expression.LessThanOrEqual(left, right);
167                         break;
168                     case OP.greater:
169                         filter = Expression.GreaterThan(left, right);
170                         break;
171                     case OP.greaterorequal:
172                         filter = Expression.GreaterThanOrEqual(left, right);
173                         break;
174                     default:
175                         break;
176                 }
177 
178 
179 
180                 var lambda = Expression.Lambda<Func<T, bool>>(filter, param);
181                 if (op == null)
182                 {
183                     op = lambda;
184                 }
185                 else
186                 {
187                     op = Expression.Lambda<Func<T, bool>>(Expression.And(op.Body, lambda.Body), op.Parameters);
188                 }
189 
190                 if (filter2 != null)
191                 {
192                     var lambda2 = Expression.Lambda<Func<T, bool>>(filter2, param);
193                     op = Expression.Lambda<Func<T, bool>>(Expression.And(op.Body, lambda2.Body), op.Parameters);
194                 }
195             }
196 
197             if (op != null)
198             {
199                 source = source.Where(op);
200             }
201             return source;
202         }
203 
204 
205         public static IQueryable<T> Where<T>(this IQueryable<T> source, Filter[] filters) where T : class
206         {
207             if (filters == null)
208             {
209                 return source;
210             }
211             Type type = typeof(T);
212 
213             ParameterExpression param = Expression.Parameter(type, "c");
214 
215             Expression<Func<T, bool>> op = null;
216 
217 
218             foreach (var rule in filters)
219             {
220                 PropertyInfo property = type.GetProperty(rule.Field);
221                 if (property == null)
222                 {
223                     continue;
224                 }
225                 //c.Field==Value
226                 //c=>(c.Field.Contains(Value) || c.Field.Contains(Value))
227                 Exception outExc = new Exception();
228 
229                 Expression left = Expression.Property(param, property);
230                 Type valueType = property.PropertyType;
231                 if (rule.Value == null || rule.Value.Length <= 0) continue;
232 
233                 Expression<Func<T, bool>> lambdaOut = null;
234                 foreach (var v in rule.Value)
235                 {
236                     Expression right = Expression.Constant(v);
237                     DateTime inputDateTime = DateTime.Now;
238                     try
239                     {
240 
241                         if (valueType == typeof(int) || valueType == typeof(int?))
242                         {
243                             right = Expression.Constant(Convert.ToInt32(v.Split('.')[0]));
244                         }
245                         else if (valueType == typeof(short) || valueType == typeof(short?))
246                         {
247                             right = Expression.Constant(Convert.ToInt16(v.Split('.')[0]));
248                         }
249                         else if (valueType == typeof(byte) || valueType == typeof(byte?))
250                         {
251                             right = Expression.Constant(Convert.ToByte(v.Split('.')[0]));
252                         }
253                         else if (valueType == typeof(long) || valueType == typeof(long?))
254                         {
255                             right = Expression.Constant(Convert.ToInt64(v));
256                         }
257                         else if (valueType == typeof(float) || valueType == typeof(float?))
258                         {
259                             right = Expression.Constant(Convert.ToSingle(v));
260                         }
261                         else if (valueType == typeof(double) || valueType == typeof(double?))
262                         {
263                             right = Expression.Constant(Convert.ToDouble(v));
264                         }
265                         else if (valueType == typeof(decimal) || valueType == typeof(decimal?))
266                         {
267                             right = Expression.Constant(Convert.ToDecimal(v));
268                         }
269                         else if (valueType == typeof(DateTime) || valueType == typeof(DateTime?))
270                         {
271                             inputDateTime = Convert.ToDateTime(v);
272                             right = Expression.Constant(Convert.ToDateTime(v));
273                         }
274                         else if (valueType == typeof(Guid) || valueType == typeof(Guid?))
275                         {
276                             right = Expression.Constant(Guid.Parse(v));
277                         }
278 
279                     }
280                     catch (Exception ex)
281                     {
282                         Console.WriteLine(ex.Message);
283                         break;
284                     }
285 
286 
287                     Expression filter = Expression.Equal(left, right);
288                     Expression filter2 = null;
289                     MethodInfo method;
290 
291                     switch (rule.Op)
292                     {
293                         case OP.contains:
294                             //BinaryExpression
295                             if (valueType == typeof(string))
296                             {
297                                 method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
298                                 filter = Expression.Call(left, method, right);
299                             }
300                             else if (valueType == typeof(DateTime) || valueType == typeof(DateTime?))
301                             {
302                                 right = Expression.Constant(inputDateTime.Date);
303                                 filter = Expression.GreaterThanOrEqual(left, right);
304                                 right = Expression.Constant(inputDateTime.Date.AddDays(1));
305                                 filter2 = Expression.LessThan(left, right);
306                             }
307                             else
308                             {
309                                 filter = Expression.Equal(left, right);
310                             }
311                             break;
312                         case OP.equal:
313                             filter = Expression.Equal(left, right);
314                             break;
315                         case OP.notequal:
316                             filter = Expression.NotEqual(left, right);
317                             break;
318                         case OP.beginwith:
319                             method = typeof(string).GetMethod("StartsWith", new[] { typeof(string) });
320 
321                             filter = Expression.Call(left, method, right);
322                             break;
323                         case OP.endwith:
324                             method = typeof(string).GetMethod("EndsWith", new[] { typeof(string) });
325 
326                             filter = Expression.Call(left, method, right);
327                             break;
328                         case OP.less:
329                             filter = Expression.LessThan(left, right);
330                             break;
331                         case OP.lessorequal:
332                             filter = Expression.LessThanOrEqual(left, right);
333                             break;
334                         case OP.greater:
335                             filter = Expression.GreaterThan(left, right);
336                             break;
337                         case OP.greaterorequal:
338                             filter = Expression.GreaterThanOrEqual(left, right);
339                             break;
340                         default:
341                             break;
342                     }
343 
344 
345 
346                     var lambda = Expression.Lambda<Func<T, bool>>(filter, param);
347                     if (lambdaOut == null)
348                     {
349                         lambdaOut = lambda;
350                     }
351                     else
352                     {
353                         lambdaOut = Expression.Lambda<Func<T, bool>>(Expression.Or(lambdaOut.Body, lambda.Body), lambdaOut.Parameters);
354                     }
355 
356                     if (filter2 != null)
357                     {
358                         var lambda2 = Expression.Lambda<Func<T, bool>>(filter2, param);
359                         lambdaOut = Expression.Lambda<Func<T, bool>>(Expression.And(lambdaOut.Body, lambda2.Body), lambdaOut.Parameters);
360                     }
361                 }
362                 if (op == null)
363                 {
364                     op = lambdaOut;
365                 }
366                 else
367                 {
368                     op = Expression.Lambda<Func<T, bool>>(Expression.And(op.Body, lambdaOut.Body), op.Parameters);
369                 }
370 
371             }
372             if (op != null)
373             {
374                 source = source.Where(op);
375             }
376             return source;
377 
378         }
379 
380         public static IQueryable<T> Where<T>(this IQueryable<T> source, string[] columnNames, string filterString)
381         {
382 
383             Type type = typeof(T);
384 
385             ParameterExpression param = Expression.Parameter(type, "c");
386 
387             Expression right = Expression.Constant(filterString);
388 
389             //1!=1
390             //Expression op = Expression.NotEqual(Expression.Constant(1), Expression.Constant(1));
391             Expression<Func<T, bool>> op = null;
392 
393 
394             foreach (var column in columnNames)
395             {
396                 PropertyInfo property = type.GetProperty(column);
397                 if (property == null)
398                 {
399                     continue;
400                 }
401                 //c.Field==Value
402                 //c=>c.Field.Contains(Value)
403                 Expression left = Expression.Property(param, property);
404 
405                 Type valueType = property.PropertyType;
406                 if (valueType != typeof(string)) continue;
407                 if (filterString == null || filterString == "") continue;
408 
409 
410                 MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
411 
412 
413                 Expression filter = Expression.Call(left, method, right);
414 
415                 var lambda = Expression.Lambda<Func<T, bool>>(filter, param);
416                 if (op == null)
417                 {
418                     op = lambda;
419                 }
420                 else
421                 {
422 
423                     op = Expression.Lambda<Func<T, bool>>(Expression.Or(op.Body, lambda.Body), op.Parameters);
424                 }
425             }
426 
427             if (op != null)
428             {
429 
430                 source = source.Where(op);
431             }
432             return source;
433         }
434     }
435 }

猜你喜欢

转载自www.cnblogs.com/hao-1234-1234/p/11271840.html