|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Linq.Expressions; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | + namespace Microsoft.EntityFrameworkCore |
| 8 | +{ |
| 9 | + public static class DbContextExtensions |
| 10 | + { |
| 11 | + public static IQueryable<T> PageAsQuery<T, TKey>(this DbContext dbContext,int pageIndex, int pageSize, out int totalCount, out int pageCount, |
| 12 | + Expression<Func<T, bool>> whereLambda, bool isAsc, Expression<Func<T, TKey>> orderBy) where T : class, new() |
| 13 | + { |
| 14 | + IQueryable<T> temp = dbContext.Set<T>().Where(whereLambda).AsQueryable(); |
| 15 | + totalCount = temp.Count(); |
| 16 | + pageSize = Math.Min(pageSize, 30); |
| 17 | + pageCount = (int)Math.Ceiling((double)totalCount / pageSize); |
| 18 | + |
| 19 | + if (isAsc) |
| 20 | + { |
| 21 | + temp = temp.OrderBy(orderBy) |
| 22 | + .Skip(pageSize * (pageIndex - 1)) |
| 23 | + .Take(pageSize).AsQueryable(); |
| 24 | + } |
| 25 | + else |
| 26 | + { |
| 27 | + temp = temp.OrderByDescending(orderBy) |
| 28 | + .Skip(pageSize * (pageIndex - 1)) |
| 29 | + .Take(pageSize).AsQueryable(); |
| 30 | + } |
| 31 | + return temp.AsNoTracking(); |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + public static IQueryable<T> PageAsQuery<T, TKey>(this DbContext dbContext,IQueryable<T> query, int pageIndex, int pageSize, out int totalCount, out int pageCount, bool isAsc, Expression<Func<T, TKey>> orderBy) where T : class, new() |
| 36 | + { |
| 37 | + IQueryable<T> temp = query; |
| 38 | + totalCount = temp.Count(); |
| 39 | + pageSize = Math.Min(pageSize, 30); |
| 40 | + pageCount = (int)Math.Ceiling((double)totalCount / pageSize); |
| 41 | + if (isAsc) |
| 42 | + { |
| 43 | + temp = temp.OrderBy(orderBy) |
| 44 | + .Skip(pageSize * (pageIndex - 1)) |
| 45 | + .Take(pageSize).AsQueryable(); |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + temp = temp.OrderByDescending(orderBy) |
| 50 | + .Skip(pageSize * (pageIndex - 1)) |
| 51 | + .Take(pageSize).AsQueryable(); |
| 52 | + } |
| 53 | + return temp; |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + public static bool Exist<T>(this DbContext dbContext,Expression<Func<T, bool>> anyLambda) where T : class, new() |
| 58 | + { |
| 59 | + return dbContext.Set<T>().Any(anyLambda); |
| 60 | + } |
| 61 | + |
| 62 | + public static T Find<T>(this DbContext dbContext,params object[] keyValues) where T : class, new() |
| 63 | + { |
| 64 | + return dbContext.Set<T>().Find(keyValues); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + public static int Count<T>(this DbContext dbContext,Expression<Func<T, bool>> countLambda) where T : class, new() |
| 69 | + { |
| 70 | + return dbContext.Set<T>().Count(countLambda); |
| 71 | + } |
| 72 | + |
| 73 | + public static T First<T>(this DbContext dbContext,Expression<Func<T, bool>> firstLambda) where T : class, new() |
| 74 | + { |
| 75 | + return dbContext.Set<T>().FirstOrDefault(firstLambda); |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + public static int SaveChanges(this DbContext dbContext) |
| 81 | + { |
| 82 | + try |
| 83 | + { |
| 84 | + return dbContext.SaveChanges(); |
| 85 | + } |
| 86 | + |
| 87 | + catch (DbUpdateException ex) |
| 88 | + { |
| 89 | + |
| 90 | + //Common.LogHelper.WriteLog(this.GetType(), ex.InnerException == null ? ex : ex.InnerException.InnerException); |
| 91 | + throw; |
| 92 | + } |
| 93 | + catch (Exception ex) |
| 94 | + { |
| 95 | + //Common.LogHelper.WriteLog(this.GetType(), ex); |
| 96 | + throw; |
| 97 | + } |
| 98 | + |
| 99 | + } |
| 100 | + } |
| 101 | +} |