/* 作者: 季健国 * 创建时间: 2012/7/17 23:55:01 * */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Linq.Expressions; using System.Data.Objects; using System.Data.EntityClient; using System.Data.SqlClient; using System.Data; namespace Ant.Service.Common { public static class LINQHelper { /// /// 分页 /// /// type /// 实现IEnumerable /// delegate检索条件 /// delegate排序 /// 每页显示数 /// 当前页码 /// returns> public static IEnumerable GetIenumberable(IEnumerable List, Func FunWhere, Func FunOrder, int PageSize, int PageIndex) { var rance = List.Where(FunWhere).OrderByDescending(FunOrder). Select(t => t).Skip((PageIndex - 1) * PageSize).Take(PageSize); return rance; } /// /// region 利用反射把DataTable的数据写到单个实体类 /// /// /// /// public static T ToSingleEntity(this System.Data.DataTable dtSource) { if (dtSource == null) { return default(T); } if (dtSource.Rows.Count != 0) { Type type = typeof(T); Object entity = Activator.CreateInstance(type); //创建实例 foreach (PropertyInfo entityCols in type.GetProperties()) { if (!string.IsNullOrEmpty(dtSource.Rows[0][entityCols.Name].ToString())) { entityCols.SetValue(entity, dtSource.Rows[0][entityCols.Name], null); } } return (T)entity; } return default(T); } /// /// 利用反射把DataTable的数据写到集合实体类里 /// /// /// /// public static IEnumerable ToListEntity(this System.Data.DataTable dtSource) { if (dtSource == null) { return null; } List list = new List(); Type type = typeof(T); foreach (DataRow dataRow in dtSource.Rows) { Object entity = Activator.CreateInstance(type); //创建实例 foreach (PropertyInfo entityCols in type.GetProperties()) { if (!string.IsNullOrEmpty(dataRow[entityCols.Name].ToString())) { entityCols.SetValue(entity, dataRow[entityCols.Name], null); } } list.Add((T)entity); } return list; } #region 删除 /// /// 执行批量删除操作。直接使用 ado.net /// /// /// /// 条件 /// public static int Delete(this ObjectQuery source, Expression> predicate) where TEntity : global::System.Data.Objects.DataClasses.EntityObject { var selectSql = (source.Where(predicate) as ObjectQuery).ToTraceString(); // int startIndex = selectSql.IndexOf(","); int endIndex = selectSql.IndexOf("."); string tableAlias = selectSql.Substring(startIndex + 1, endIndex - startIndex - 1);//get table alias startIndex = selectSql.IndexOf("FROM"); string deleteSql = "DELETE " + tableAlias + " " + selectSql.Substring(startIndex); //Gets the string used to open a SQL Server database string connectionString = ((source as ObjectQuery).Context.Connection as EntityConnection).StoreConnection.ConnectionString; return ExecuteNonQuery(connectionString, deleteSql); } /// /// 执行T-sql 语句 /// /// /// /// /// private static int ExecuteNonQuery(string connectionString, string sql, params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(connectionString)) { SqlCommand cmd = conn.CreateCommand(); cmd.Parameters.AddRange(parameters); cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = sql; if (conn.State != System.Data.ConnectionState.Open) { conn.Open(); } return cmd.ExecuteNonQuery(); } } #endregion } }