当前位置:Gxlcms > 数据库问题 > 用Linq To SQL 搭建底层

用Linq To SQL 搭建底层

时间:2021-07-01 10:21:17 帮助过:4人阅读

用Linq To SQL 搭建底层

接口

  1. <code>using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Linq.Expressions;
  7. namespace Project.DAL
  8. {
  9. interface IBaseService<T> where T:class,new()
  10. {
  11. IQueryable<T> QueryAll(params Expression<Func<T, bool>>[] where);
  12. IQueryable<T> QueryAll<type>(Expression<Func<T, type>> order, bool isAsc = true, params Expression<Func<T, bool>>[] where);
  13. IQueryable<T> QueryAll<type>(out int total, int skip = 0, int take = 10, Expression<Func<T, type>> order = null, bool isAsc = true, params Expression<Func<T, bool>>[] where);
  14. void Insert(T t);
  15. void Insert(IEnumerable<T> t);
  16. void Delete(T t);
  17. void Delete(IEnumerable<T> t);
  18. bool SaveChange();
  19. }
  20. }</code>

底层

  1. <code>using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Linq.Expressions;
  7. namespace Project.DAL
  8. {
  9. public class BaseService<T> : IBaseService<T> where T : class, new()
  10. {
  11. //上下文对象
  12. Models.QuestionDBDataContext dbContext = new Models.QuestionDBDataContext();//直接复制会报错应先在Models里引入数据库
  13. private IQueryable<T> table = null;
  14. private IQueryable<T> GetTable()
  15. {
  16. if (table == null)
  17. {
  18. table = (IQueryable<T>)dbContext.GetTable<T>();
  19. }
  20. return table;
  21. }
  22. /// <summary>
  23. /// 查询
  24. /// </summary>
  25. /// <param name="where">查询条件</param>
  26. /// <returns></returns>
  27. public IQueryable<T> QueryAll(params Expression<Func<T, bool>>[] where)
  28. {
  29. IQueryable<T> iq = GetTable();
  30. if (where != null || where.Length < 1)
  31. {
  32. foreach (var item in where)
  33. {
  34. iq = iq.Where(item);
  35. }
  36. }
  37. return iq;
  38. }
  39. /// <summary>
  40. /// 排序
  41. /// </summary>
  42. /// <typeparam name="type">排序类型</typeparam>
  43. /// <param name="order">排序列</param>
  44. /// <param name="isAsc">升序or降序</param>
  45. /// <param name="where"></param>
  46. /// <returns></returns>
  47. public IQueryable<T> QueryAll<type>(Expression<Func<T, type>> order, bool isAsc = true, params Expression<Func<T, bool>>[] where)
  48. {
  49. var iq = QueryAll(where);
  50. if (isAsc)
  51. {
  52. iq = iq.OrderBy(order);
  53. }
  54. else
  55. {
  56. iq = iq.OrderByDescending(order);
  57. }
  58. return iq;
  59. }
  60. /// <summary>
  61. /// 分页查询
  62. /// </summary>
  63. /// <typeparam name="type"></typeparam>
  64. /// <param name="total">总数据数</param>
  65. /// <param name="skip">跳过n条</param>
  66. /// <param name="take">取n条(一页显示几条)</param>
  67. /// <param name="order"></param>
  68. /// <param name="isAsc"></param>
  69. /// <param name="where"></param>
  70. /// <returns></returns>
  71. public IQueryable<T> QueryAll<type>(out int total, int skip = 0, int take = 10, Expression<Func<T, type>> order = null, bool isAsc = true, params Expression<Func<T, bool>>[] where)
  72. {
  73. var iq = QueryAll<type>(order, isAsc, where);
  74. total = iq.Count();
  75. return iq.Skip(skip).Take(take);
  76. }
  77. public void Insert(T t)
  78. {
  79. dbContext.GetTable<T>().InsertOnSubmit(t);
  80. }
  81. public void Insert(IEnumerable<T> t)
  82. {
  83. dbContext.GetTable<T>().InsertAllOnSubmit(t);
  84. }
  85. public void Delete(T t)
  86. {
  87. dbContext.GetTable<T>().DeleteOnSubmit(t);
  88. }
  89. public void Delete(IEnumerable<T> t)
  90. {
  91. dbContext.GetTable<T>().DeleteAllOnSubmit(t);
  92. }
  93. /// <summary>
  94. /// 保存修改
  95. /// </summary>
  96. /// <returns></returns>
  97. public bool SaveChange()
  98. {
  99. try
  100. {
  101. dbContext.SubmitChanges();
  102. return true;
  103. }
  104. catch (Exception e)
  105. {
  106. return false;
  107. }
  108. }
  109. }</code>

}

用Linq To SQL 搭建底层

标签:get   except   namespace   ext   context   code   reading   查询条件   private   

人气教程排行