LogRepository.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Linq.Expressions;
  6. using System.Data.Entity;
  7. using System.Data.Entity.Infrastructure;
  8. using Bowin.Common.Data;
  9. using Bowin.Common.Linq.Entity;
  10. namespace EMIS.DataLogic.Repositories.Log
  11. {
  12. public class LogRepository<TEntity> where TEntity : class
  13. {
  14. private LogUnitOfWork _unitOfWork;
  15. private LogRepository()
  16. {
  17. }
  18. public LogRepository(LogUnitOfWork unitOfWork)
  19. {
  20. this._unitOfWork = unitOfWork;
  21. }
  22. public TEntity GetSingle(Expression<Func<TEntity, bool>> expression)
  23. {
  24. return this.Entities.Where<TEntity>(expression).FirstOrDefault<TEntity>();
  25. }
  26. public TEntity GetSingle(Expression<Func<TEntity, bool>> expression, params string[] paths)
  27. {
  28. IQueryable<TEntity> source = this.Entities;//.Include(path)
  29. foreach (var path in paths)
  30. {
  31. source = source.Include(path);
  32. }
  33. return source.Where<TEntity>(expression).FirstOrDefault<TEntity>();
  34. }
  35. public TEntity GetSingle(Expression<Func<TEntity, bool>> expression, params Expression<Func<TEntity, object>>[] paths)
  36. {
  37. IQueryable<TEntity> source = this.Entities;//.Include(path)
  38. foreach (var path in paths)
  39. {
  40. source = source.Include(path);
  41. }
  42. return source.Where<TEntity>(expression).FirstOrDefault<TEntity>();
  43. }
  44. public IQueryable<TEntity> Entities
  45. {
  46. get
  47. {
  48. return this._unitOfWork.Set<TEntity>();
  49. }
  50. }
  51. public LogUnitOfWork UnitOfWork
  52. {
  53. get
  54. {
  55. return this._unitOfWork;
  56. }
  57. }
  58. 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)
  59. {
  60. IQueryable<TEntity> source = this.Entities;//.Include(path)
  61. foreach (var path in paths)
  62. {
  63. source = source.Include(path);
  64. }
  65. source = source.Where<TEntity>(expression);
  66. source = descending ? source.OrderByDescending<TEntity, S>(orderBy) : source.OrderBy<TEntity, S>(orderBy);
  67. GridResultSet<TEntity> gridResult = new GridResultSet<TEntity>();
  68. gridResult.rows = source.Skip<TEntity>((pageIndex) * pageSize).Take<TEntity>(pageSize).ToList();
  69. gridResult.total = source.Count();
  70. return gridResult;
  71. }
  72. public GridResultSet<TEntity> GetPagedList<S>(Expression<Func<TEntity, bool>> expression, int pageIndex, int pageSize, Expression<Func<TEntity, S>> orderBy, bool descending)
  73. {
  74. try
  75. {
  76. IQueryable<TEntity> source = this.Entities.Where<TEntity>(expression);
  77. source = descending ? source.OrderByDescending<TEntity, S>(orderBy) : source.OrderBy<TEntity, S>(orderBy);
  78. GridResultSet<TEntity> gridResult = new GridResultSet<TEntity>();
  79. gridResult.rows = source.Skip<TEntity>((pageIndex) * pageSize).Take<TEntity>(pageSize).ToList();
  80. gridResult.total = source.Count();
  81. return gridResult;
  82. }
  83. catch (Exception ex)
  84. {
  85. throw ex;
  86. }
  87. }
  88. public IQueryable<TEntity> GetList<S>(Expression<Func<TEntity, bool>> expression, Expression<Func<TEntity, S>> orderBy, bool descending)
  89. {
  90. var source = GetList(expression);
  91. return descending ? source.OrderByDescending<TEntity, S>(orderBy) : source.OrderBy<TEntity, S>(orderBy);
  92. }
  93. public IQueryable<TEntity> GetList(Expression<Func<TEntity, bool>> expression)
  94. {
  95. return this.Entities.Where<TEntity>(expression);
  96. }
  97. }
  98. }