using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.Core.Common.CommandTrees; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.Entity.Migrations; using System.Linq.Expressions; using CK.SCP.Models; namespace CK.SCP.Controller { public static class EntitiesHelper { public static List<dynamic> GetModeList_genaral<T, TKey>(Expression<Func<T, dynamic>> select, Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, out int count) where T : class { List<dynamic> list = null; count = 0; using (DbContext db = EntitiesFactory.CreateScpInstance()) { count = db.Set<T>().Where(where).Count(); list = db.Set<T>() .Where(where) .OrderBy(order) .Select(select).ToList(); } return list; } public static List<dynamic> GetPagedModelListAsc<T, TKey>(Expression<Func<T, dynamic>> select, Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, int pageIndex, int pageSize, out int total) where T : class { List<dynamic> list = null; total = 0; using (DbContext db = EntitiesFactory.CreateScpInstance()) { total = db.Set<T>().Where(where).Count(); list = db.Set<T>() .Where(where) .OrderBy(order) .Select(select) .Skip((pageIndex - 1) * pageSize) .Take(pageSize).ToList(); } return list; } public static List<T> GetPagedDataAsc<T, TKey>(Expression<Func<T, T>> select, Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, int pageIndex, int pageSize, out int total) where T : class { List<T> list = null; total = 0; using (DbContext db = EntitiesFactory.CreateScpInstance()) { total = db.Set<T>().Where(where.Compile()).AsQueryable().Count(); list = db.Set<T>() .Where(where.Compile()).AsQueryable() .OrderBy(order) .Select(select) .Skip((pageIndex - 1) * pageSize) .Take(pageSize).ToList(); } return list; } //TODO 分页 原来 pageIndex-1 public static List<dynamic> GetPagedDataDesc<T, TKey>(DbContext db, Expression<Func<T, dynamic>> select, Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, int pageIndex, int pageSize, out int total) where T : class { List<dynamic> list = null; total = 0; total = db.Set<T>().Where(where).Count(); list = db.Set<T>() .Where(where) .OrderByDescending(order) .Select(select) .Skip((pageIndex) * pageSize) .Take(pageSize).ToList(); return list; } public static List<T> GetPagedList<T, TKey>(DbContext db, Expression<Func<T, TKey>> order, int pageIndex, int pageSize, out int total) where T : class { List<T> list = null; total = 0; total = db.Set<T>().Count(); list = db.Set<T>() .OrderByDescending(order) .Skip((pageIndex) * pageSize) .Take(pageSize).ToList(); return list; } } }