/// <summary> /// 分页查询(Linq分页方式) /// </summary> /// <param cookieName="pageNumber">当前页</param> /// <param cookieName="pageSize">页码</param> /// <param cookieName="orderName">lambda排序名称</param> /// <param cookieName="sortOrder">排序(升序or降序)</param> /// <param cookieName="exp">lambda查询条件where</param> /// <returns></returns> public virtual IEnumerable <T> GetEntitiesForPaging(int pageNumber, int pageSize, Func <T, string> orderName, string sortOrder, Func <T, bool> exp) { using (KS_StuInfoEntities Entities = new KS_StuInfoEntities()) { if (sortOrder == "asc") //升序排列 { return(Entities.Set <T>().Where(exp).OrderBy(orderName).Skip((pageNumber - 1) * pageSize) .Take(pageSize).ToList()); } else { return(Entities.Set <T>().Where(exp).OrderByDescending(orderName).Skip((pageNumber - 1) * pageSize) .Take(pageSize).ToList()); } } }
public virtual IEnumerable <U> GetEntities <U>(Func <T, bool> exp, Func <T, U> select) { using (KS_StuInfoEntities Entities = new KS_StuInfoEntities()) { var querys = Entities.Set <T>().Where(exp).Select(select); //string sql = (((System.Data.Objects.ObjectQuery)querys.AsQueryable()).ToTraceString()); return(querys.ToList()); } }
public virtual U GetEntity <U>(Func <T, bool> exp, Func <T, U> select) { using (KS_StuInfoEntities Entities = new KS_StuInfoEntities()) { var entity = Entities.Set <T>().Where(exp).Select(select).SingleOrDefault(); //if (entity != null) //{ // Entities.Entry<T>(entity).State = System.Data.Entity.EntityState.Detached; //} return(entity); } }
/// <summary> /// 根据条件查找 /// </summary> /// <param cookieName="exp">lambda查询条件where</param> /// <returns></returns> public virtual T GetEntity(Func <T, bool> exp) { using (KS_StuInfoEntities Entities = new KS_StuInfoEntities()) { var entity = Entities.Set <T>().Where(exp).SingleOrDefault(); if (entity != null) { Entities.Entry <T>(entity).State = System.Data.Entity.EntityState.Detached; } return(entity); } }
/// <summary> /// 计算总个数(分页) /// </summary> /// <param cookieName="exp">Lambda条件的where</param> /// <returns></returns> public virtual int GetEntitiesCount(Func <T, bool> exp) { using (KS_StuInfoEntities Entities = new KS_StuInfoEntities()) { try { return(Entities.Set <T>().Count(exp)); } catch (Exception) { return(0); } } }
/// <summary> /// 获取所有Entity /// </summary> /// <param cookieName="exp">Lambda条件的where</param> /// <returns></returns> public virtual List <T> GetEntities(Func <T, bool> exp) { using (KS_StuInfoEntities Entities = new KS_StuInfoEntities()) { try { var data = Entities.Set <T>().Where(exp).ToList(); return(data); } catch (Exception e) { throw e; } } }