123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data.Entity;
- using System.Linq.Dynamic;
- using System.Linq.Expressions;
- using System.Data.Entity.Infrastructure;
- //using System.Data.Objects;
- using System.Reflection;
- using System.Data;
- using System.Collections;
- using System.Data.Entity.Core.Objects;
- using System.Web;
- namespace Bowin.Common.Linq.Entity
- {
- /// <summary>
- /// Helper extensions for add IDbSet methods defined only
- /// for DbSet and ObjectQuery
- /// </summary>
- public static class IQueryableExtensions
- {
- /// <summary>
- /// 映射。
- /// </summary>
- /// <typeparam name="TSource"></typeparam>
- /// <param name="queryable"></param>
- /// <returns></returns>
- public static ExpressionFactory<TSource> Map<TSource>(this IQueryable<TSource> queryable)
- {
- return new ExpressionFactory<TSource>(queryable);
- }
- /// <summary>
- /// Include extension method for IDbSet
- /// <example>
- /// var query = ReturnTheQuery();
- /// query = query.Include(customer=>customer.Orders);//"Orders"
- /// //or
- /// query = query.Include(customer=>customer.Orders.Select(o=>o.OrderDetails) //"Orders.OrderDetails"
- /// </example>
- /// </summary>
- /// <typeparam name="TEntity">Type of elements in IQueryable</typeparam>
- /// <typeparam name="TEntity">Type of navigated element</typeparam>
- /// <param name="queryable">Queryable object</param>
- /// <param name="path">Expression with path to include</param>
- /// <returns>Queryable object with include path information</returns>
- public static IQueryable<TEntity> Include<TEntity, TProperty>(this IDbSet<TEntity> queryable, Expression<Func<TEntity, TProperty>> path)
- where TEntity : class
- {
- var objectQuery = queryable as DbQuery<TEntity>;
- if (objectQuery != null) //Is DBSET
- {
- return objectQuery.Include(path);
- }
- else // probably mock
- return queryable;
- }
- /// <summary>
- /// OfType extension method for IQueryable
- /// </summary>
- /// <typeparam name="KEntity">The type to filter the elements of the sequence on. </typeparam>
- /// <param name="queryable">The queryable object</param>
- /// <returns>
- /// A new IQueryable hat contains elements from
- /// the input sequence of type TResult
- /// </returns>
- public static IQueryable<KEntity> OfType<TEntity, KEntity>(this IQueryable<TEntity> queryable)
- where TEntity : class
- where KEntity : class,TEntity
- {
- var objectQuery = queryable as DbQuery<TEntity>;
- if (objectQuery != null) //is DBSET
- {
- return objectQuery.OfType<KEntity>();
- }
- else // probably IDbSet Mock
- return queryable.OfType<KEntity>();
- }
- public static IGridResultSet<TEntity> ToGridResultSet<TEntity>(this IQueryable<TEntity> dataSource, int? pageIndex = null, int? pageSize = null)
- {
- var orderby = (OrderByStatementView)HttpContext.Current.Session["Bowin_Common_Linq_Entity_CurOrderby"];
- if (orderby != null)
- {
- if (orderby.isAsc)
- {
- dataSource = dataSource.OrderBy(orderby.OrderBy);
- }
- else
- {
- dataSource = dataSource.OrderBy(orderby.OrderBy + " desc");
- }
- HttpContext.Current.Session["Bowin_Common_Linq_Entity_CurOrderby"] = null;
- }
- if (pageIndex.HasValue && pageSize.HasValue)
- {
- return new GridResultSet<TEntity>
- {
- rows = dataSource.Skip(pageIndex.Value * pageSize.Value).Take(pageSize.Value).ToList(),
- total = dataSource.Count()
- };
- }
- else
- {
- var result = new GridResultSet<TEntity>
- {
- rows = dataSource.ToList()
- };
- result.total = result.rows.Count;
- return result;
- }
- }
- public static IQueryable<TEntity> DynamicWhere<TEntity>(this IQueryable<TEntity> dataSource, string propertyName, string comparer, object value)
- {
- ExpressionFactory<TEntity> factory = new ExpressionFactory<TEntity>(dataSource);
- return dataSource.Where(factory.DynamicExpression(propertyName, comparer, value));
- }
- public static IQueryable<TEntity> DynamicInWhere<TEntity, TListType>(this IQueryable<TEntity> dataSource, string propertyName, IList<TListType> value)
- {
- ExpressionFactory<TEntity> factory = new ExpressionFactory<TEntity>(dataSource);
- return dataSource.Where(factory.DynamicInExpression(propertyName, value));
- }
- /// <summary>
- /// 分页查询,当 pageIndex 或 pageSize 为null时,获取全部数据。
- /// </summary>
- /// <typeparam name="TEntity">实体类型</typeparam>
- /// <param name="query">分页数据源</param>
- /// <param name="pageIndex">页码,从1开始</param>
- /// <param name="pageSize">页数</param>
- /// <returns></returns>
- public static PageList<TEntity> ToPageList<TEntity>(this IQueryable<TEntity> query, int? pageIndex, int? pageSize)
- {
- return new PageList<TEntity>(query, pageIndex, pageSize);
- }
- public static ObjectParameterCollection GetParamters<TEntity>(this IQueryable<TEntity> linqBody)
- {
- var internalQueryField = linqBody.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(f => f.Name.Equals("_internalSet")).FirstOrDefault();
- if (internalQueryField == null)
- {
- internalQueryField = linqBody.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(f => f.Name.Equals("_internalQuery")).FirstOrDefault();
- }
- var internalQuery = internalQueryField.GetValue(linqBody);
- var objectQueryField = internalQuery.GetType().GetProperties().Where(f => f.Name.Equals("ObjectQuery")).FirstOrDefault();
- var objectQuery = objectQueryField.GetValue(internalQuery, null) as ObjectQuery<TEntity>;
- var paras = objectQuery.Parameters;
- return paras;
- }
- public static IDbConnection GetConnection(this IQueryable queryable)
- {
- var internalQueryField = queryable.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(f => f.Name.Equals("_internalSet")).FirstOrDefault();
- if (internalQueryField == null)
- {
- internalQueryField = queryable.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(f => f.Name.Equals("_internalQuery")).FirstOrDefault();
- }
- var internalQuery = internalQueryField.GetValue(queryable);
- var internalContextField = internalQuery.GetType().GetProperties().Where(f => f.Name.Equals("InternalContext")).FirstOrDefault();
- var internalContext = internalContextField.GetValue(internalQuery, null);
- var internalConnectionField = internalContext.GetType().GetProperties().Where(p => p.Name.Equals("Connection")).FirstOrDefault();
- var internalConnection = internalConnectionField.GetValue(internalContext, null);
- //var underlyingConnectionProperty = internalConnection.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance).Where(p => p.Name.Equals("UnderlyingConnection")).FirstOrDefault();
- //var underlyingConnection = underlyingConnectionProperty.GetValue(internalConnection, null);
- return (IDbConnection)internalConnection;
- }
- public static string GetConnectionKey(this IQueryable queryable)
- {
- var internalQueryField = queryable.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(f => f.Name.Equals("_internalSet")).FirstOrDefault();
- if (internalQueryField == null)
- {
- internalQueryField = queryable.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(f => f.Name.Equals("_internalQuery")).FirstOrDefault();
- }
- var internalQuery = internalQueryField.GetValue(queryable);
- var internalContextField = internalQuery.GetType().GetProperties().Where(f => f.Name.Equals("InternalContext")).FirstOrDefault();
- var internalContext = internalContextField.GetValue(internalQuery, null);
- var objectContextProp = internalContext.GetType().GetProperty("ConnectionStringName", BindingFlags.Public | BindingFlags.Instance);
- string contextName = objectContextProp.GetValue(internalContext, null).ToString();
- return contextName;
- }
- public static string GetDbConnectionKey(this DbContext db)
- {
- var internalContextField = typeof(System.Data.Entity.DbContext).GetField("_internalContext", BindingFlags.NonPublic | BindingFlags.Instance);
- var internalContext = internalContextField.GetValue(db);
- var objectContextProp = internalContext.GetType().GetProperty("ConnectionStringName", BindingFlags.Public | BindingFlags.Instance);
- string contextName = objectContextProp.GetValue(internalContext, null).ToString();
- return contextName;
- }
- public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string propertyName, bool ascending) where T : class
- {
- Type type = typeof(T);
- PropertyInfo property = type.GetProperty(propertyName);
- if (property == null)
- throw new ArgumentException("propertyName", "Not Exist");
- ParameterExpression param = Expression.Parameter(type, "p");
- Expression propertyAccessExpression = Expression.MakeMemberAccess(param, property);
- LambdaExpression orderByExpression = Expression.Lambda(propertyAccessExpression, param);
- string methodName = ascending ? "OrderBy" : "OrderByDescending";
- MethodCallExpression exp = Expression.Call(
- typeof(Queryable)
- , methodName
- , new Type[] { type, property.PropertyType }
- , source.Expression
- , Expression.Quote(orderByExpression));
- return source.Provider.CreateQuery<T>(exp);
- }
- /// <summary>
- /// 将集合类转换成DataTable
- /// </summary>
- /// <param name="list">集合</param>
- /// <returns></returns>
- public static DataTable ToDataTable<T>(this List<T> list)
- {
- DataTable result = new DataTable();
- if (list.Count > 0)
- {
- PropertyInfo[] propertys = list[0].GetType().GetProperties();
- foreach (PropertyInfo pi in propertys)
- {
- result.Columns.Add(pi.Name, pi.PropertyType);
- }
- for (int i = 0; i < list.Count; i++)
- {
- ArrayList tempList = new ArrayList();
- foreach (PropertyInfo pi in propertys)
- {
- object obj = pi.GetValue(list[i], null);
- tempList.Add(obj);
- }
- object[] array = tempList.ToArray();
- result.LoadDataRow(array, true);
- }
- }
- return result;
- }
- }
- }
|