123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Linq.Expressions;
- using System.Data.Entity;
- using System.Data.Entity.Infrastructure;
- using Bowin.Common.Data;
- using Bowin.Common.Linq.Entity;
- namespace EMISOnline.DataLogic.Repositories
- {
- public class Repository<TEntity> where TEntity : class
- {
- private UnitOfWork _unitOfWork;
- private Repository()
- {
- }
- public Repository(UnitOfWork unitOfWork)
- {
- this._unitOfWork = unitOfWork;
- }
- public TEntity GetSingle(Expression<Func<TEntity, bool>> expression)
- {
- return this.Entities.Where<TEntity>(expression).FirstOrDefault<TEntity>();
- }
- public TEntity GetSingle(Expression<Func<TEntity, bool>> expression, params string[] paths)
- {
- IQueryable<TEntity> source = this.Entities;//.Include(path)
- foreach (var path in paths)
- {
- source = source.Include(path);
- }
- return source.Where<TEntity>(expression).FirstOrDefault<TEntity>();
- }
- public TEntity GetSingle(Expression<Func<TEntity, bool>> expression, params Expression<Func<TEntity, object>>[] paths)
- {
- IQueryable<TEntity> source = this.Entities;//.Include(path)
- foreach (var path in paths)
- {
- source = source.Include(path);
- }
- return source.Where<TEntity>(expression).FirstOrDefault<TEntity>();
- }
- public IQueryable<TEntity> Entities
- {
- get
- {
- return this._unitOfWork.Set<TEntity>();
- }
- }
- public UnitOfWork UnitOfWork
- {
- get
- {
- return this._unitOfWork;
- }
- }
- public GridResultSet<TEntity> GetPagedList<S>(Expression<Func<TEntity, bool>> expression, int pageIndex, int pageSize, Expression<Func<TEntity, S>> orderBy, bool descending, params Expression<Func<TEntity, object>>[] paths)
- {
- IQueryable<TEntity> source = this.Entities;//.Include(path)
- foreach (var path in paths)
- {
- source = source.Include(path);
- }
- source = source.Where<TEntity>(expression);
- source = descending ? source.OrderByDescending<TEntity, S>(orderBy) : source.OrderBy<TEntity, S>(orderBy);
- GridResultSet<TEntity> gridResult = new GridResultSet<TEntity>();
- gridResult.rows = source.Skip<TEntity>((pageIndex) * pageSize).Take<TEntity>(pageSize).ToList();
- gridResult.total = source.Count();
- return gridResult;
- }
- public GridResultSet<TEntity> GetPagedList<S>(Expression<Func<TEntity, bool>> expression, int pageIndex, int pageSize, Expression<Func<TEntity, S>> orderBy, bool descending)
- {
- try
- {
- IQueryable<TEntity> source = this.Entities.Where<TEntity>(expression);
- source = descending ? source.OrderByDescending<TEntity, S>(orderBy) : source.OrderBy<TEntity, S>(orderBy);
- GridResultSet<TEntity> gridResult = new GridResultSet<TEntity>();
- gridResult.rows = source.Skip<TEntity>((pageIndex) * pageSize).Take<TEntity>(pageSize).ToList();
- gridResult.total = source.Count();
- return gridResult;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public IQueryable<TEntity> GetList<S>(Expression<Func<TEntity, bool>> expression, Expression<Func<TEntity, S>> orderBy, bool descending)
- {
- var source = GetList(expression);
- return descending ? source.OrderByDescending<TEntity, S>(orderBy) : source.OrderBy<TEntity, S>(orderBy);
- }
- public IQueryable<TEntity> GetList(Expression<Func<TEntity, bool>> expression)
- {
- return this.Entities.Where<TEntity>(expression);
- }
- public IQueryable<TEntity> GetList(Expression<Func<TEntity, bool>> expression, params string[] paths)
- {
- IQueryable<TEntity> source = this.Entities;//.Include(path)
- foreach (var path in paths)
- {
- source = source.Include(path);
- }
- return source.Where<TEntity>(expression);
- }
- public IQueryable<TEntity> GetList(Expression<Func<TEntity, bool>> expression, params Expression<Func<TEntity, object>>[] paths)
- {
- IQueryable<TEntity> source = this.Entities;//.Include(path)
- foreach (var path in paths)
- {
- source = source.Include(path);
- }
- return source.Where<TEntity>(expression);
- }
- }
- }
|