LINQHelper.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* 作者: 季健国
  2. * 创建时间: 2012/7/17 23:55:01
  3. *
  4. */
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Reflection;
  10. using System.Linq.Expressions;
  11. using System.Data.Objects;
  12. using System.Data.EntityClient;
  13. using System.Data.SqlClient;
  14. using System.Data;
  15. namespace Ant.Service.Common
  16. {
  17. public static class LINQHelper
  18. {
  19. /// <summary>
  20. /// 分页
  21. /// <summary>
  22. /// <typeparam name="T">type<param>
  23. /// <param name="List">实现IEnumerable<param>
  24. /// <param name="FunWhere">delegate检索条件<param>
  25. /// <param name="FunOrder">delegate排序<param>
  26. /// <param name="PageSize">每页显示数<param>
  27. /// <param name="PageIndex">当前页码<param>
  28. /// <returns>returns>
  29. public static IEnumerable<T> GetIenumberable<T>(IEnumerable<T> List, Func<T,
  30. bool> FunWhere, Func<T, string> FunOrder, int PageSize, int PageIndex)
  31. {
  32. var rance = List.Where(FunWhere).OrderByDescending(FunOrder).
  33. Select(t => t).Skip((PageIndex - 1) * PageSize).Take(PageSize);
  34. return rance;
  35. }
  36. /// <summary>
  37. /// region 利用反射把DataTable的数据写到单个实体类
  38. /// </summary>
  39. /// <typeparam name="T"></typeparam>
  40. /// <param name="dtSource"></param>
  41. /// <returns></returns>
  42. public static T ToSingleEntity<T>(this System.Data.DataTable dtSource)
  43. {
  44. if (dtSource == null)
  45. {
  46. return default(T);
  47. }
  48. if (dtSource.Rows.Count != 0)
  49. {
  50. Type type = typeof(T);
  51. Object entity = Activator.CreateInstance(type); //创建实例
  52. foreach (PropertyInfo entityCols in type.GetProperties())
  53. {
  54. if (!string.IsNullOrEmpty(dtSource.Rows[0][entityCols.Name].ToString()))
  55. {
  56. entityCols.SetValue(entity, dtSource.Rows[0][entityCols.Name], null);
  57. }
  58. }
  59. return (T)entity;
  60. }
  61. return default(T);
  62. }
  63. /// <summary>
  64. /// 利用反射把DataTable的数据写到集合实体类里
  65. /// </summary>
  66. /// <typeparam name="T"></typeparam>
  67. /// <param name="dtSource"></param>
  68. /// <returns></returns>
  69. public static IEnumerable<T> ToListEntity<T>(this System.Data.DataTable dtSource)
  70. {
  71. if (dtSource == null)
  72. {
  73. return null;
  74. }
  75. List<T> list = new List<T>();
  76. Type type = typeof(T);
  77. foreach (DataRow dataRow in dtSource.Rows)
  78. {
  79. Object entity = Activator.CreateInstance(type); //创建实例
  80. foreach (PropertyInfo entityCols in type.GetProperties())
  81. {
  82. if (!string.IsNullOrEmpty(dataRow[entityCols.Name].ToString()))
  83. {
  84. entityCols.SetValue(entity, dataRow[entityCols.Name], null);
  85. }
  86. }
  87. list.Add((T)entity);
  88. }
  89. return list;
  90. }
  91. #region 删除
  92. /// <summary>
  93. /// 执行批量删除操作。直接使用 ado.net
  94. /// </summary>
  95. /// <typeparam name="TEntity"></typeparam>
  96. /// <param name="source"></param>
  97. /// <param name="predicate">条件</param>
  98. /// <returns></returns>
  99. public static int Delete<TEntity>(this ObjectQuery<TEntity> source, Expression<Func<TEntity, bool>> predicate)
  100. where TEntity : global::System.Data.Objects.DataClasses.EntityObject
  101. {
  102. var selectSql = (source.Where(predicate) as ObjectQuery).ToTraceString();
  103. //
  104. int startIndex = selectSql.IndexOf(",");
  105. int endIndex = selectSql.IndexOf(".");
  106. string tableAlias = selectSql.Substring(startIndex + 1, endIndex - startIndex - 1);//get table alias
  107. startIndex = selectSql.IndexOf("FROM");
  108. string deleteSql = "DELETE " + tableAlias + " " + selectSql.Substring(startIndex);
  109. //Gets the string used to open a SQL Server database
  110. string connectionString = ((source as ObjectQuery).Context.Connection as EntityConnection).StoreConnection.ConnectionString;
  111. return ExecuteNonQuery(connectionString, deleteSql);
  112. }
  113. /// <summary>
  114. /// 执行T-sql 语句
  115. /// </summary>
  116. /// <param name="connectionString"></param>
  117. /// <param name="sql"></param>
  118. /// <param name="parameters"></param>
  119. /// <returns></returns>
  120. private static int ExecuteNonQuery(string connectionString, string sql, params SqlParameter[] parameters)
  121. {
  122. using (SqlConnection conn = new SqlConnection(connectionString))
  123. {
  124. SqlCommand cmd = conn.CreateCommand();
  125. cmd.Parameters.AddRange(parameters);
  126. cmd.CommandType = System.Data.CommandType.Text;
  127. cmd.CommandText = sql;
  128. if (conn.State != System.Data.ConnectionState.Open)
  129. {
  130. conn.Open();
  131. }
  132. return cmd.ExecuteNonQuery();
  133. }
  134. }
  135. #endregion
  136. }
  137. }