123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Reflection;
- using System.Data;
- using System.Collections;
- using Bowin.Common.Utility;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.EntityFrameworkCore.Metadata.Conventions;
- using Newtonsoft.Json.Linq;
- using Newtonsoft.Json;
- 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);
- }
- public static IGridResultSet<TEntity> ToGridResultSet<TEntity>(this IOrderedEnumerable<TEntity> dataSource, int? pageIndex = null, int? pageSize = null)
- {
- if (pageIndex.HasValue && pageSize.HasValue)
- {
- return new GridResultSet<TEntity>
- {
- rows = dataSource.Skip((pageIndex.Value - 1) * 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 IGridResultSet<TEntity> ToGridResultSet<TEntity>(this IOrderedQueryable<TEntity> dataSource, int? pageIndex = null, int? pageSize = null)
- {
- if (pageIndex.HasValue && pageSize.HasValue)
- {
- return new GridResultSet<TEntity>
- {
- rows = dataSource.Skip((pageIndex.Value - 1) * pageSize.Value).Take(pageSize.Value).ToList(),
- //rows = dataSource.Select((x, i) => new { RowNumber = i, Data = x })
- // .Where(x => x.RowNumber > pageIndex.Value * pageSize.Value).Take(pageSize.Value).Select(x => x.Data).ToList(),
- total = dataSource.Count()
- };
- }
- else
- {
- var result = new GridResultSet<TEntity>
- {
- rows = dataSource.ToList()
- };
- result.total = result.rows.Count;
- return result;
- }
- }
- public static IGridResultSet<TEntity> ToGridResultSet<TEntity>(this IQueryable<TEntity> dataSource, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderByAction, int? pageIndex = null, int? pageSize = null)
- {
- if (pageIndex.HasValue && pageSize.HasValue)
- {
- return new GridResultSet<TEntity>
- {
- rows = orderByAction.Invoke(orderByAction.Invoke(dataSource).Skip((pageIndex.Value - 1) * pageSize.Value).Take(pageSize.Value)).ToList(),
- //rows = dataSource.Select((x, i) => new { RowNumber = i, Data = x })
- // .Where(x => x.RowNumber > pageIndex.Value * pageSize.Value).Take(pageSize.Value).Select(x => x.Data).ToList(),
- total = dataSource.Count()
- };
- }
- else
- {
- var result = new GridResultSet<TEntity>
- {
- rows = dataSource.ToList()
- };
- result.total = result.rows.Count;
- return result;
- }
- }
- public static List<TResult> GridResultSetJoin<TEntity, TInner, TKey, TResult>(this IQueryable<TEntity> dataSource, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderByAction,
- IQueryable<TInner> innerQueryable, Expression<Func<TEntity, TKey>> innerKeySelector, Expression<Func<TInner, TKey>> outerKeySelector, Expression<Func<TEntity, TInner, TResult>> resultSelector,
- int? pageIndex = null, int? pageSize = null)
- {
- if (pageIndex.HasValue && pageSize.HasValue)
- {
- return orderByAction.Invoke(orderByAction.Invoke(dataSource).Skip((pageIndex.Value - 1) * pageSize.Value).Take(pageSize.Value))
- .Join(innerQueryable, innerKeySelector, outerKeySelector, resultSelector).ToList();
- }
- else
- {
- return dataSource.Join(innerQueryable, innerKeySelector, outerKeySelector, resultSelector).ToList();
- }
- }
- public static IGridResultSet<JObject> ToJsonResultSet<TEntity>(this IOrderedQueryable<TEntity> dataSource, int? pageIndex = null, int? pageSize = null)
- {
- var resultSet = ToGridResultSet(dataSource, pageIndex, pageSize);
- return new GridResultSet<JObject>
- {
- rows = resultSet.rows.Select(x => JObject.FromObject(x)).ToList(),
- total = resultSet.total
- };
- }
- 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));
- }
- 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);
- }
- public static IQueryable<T> ThenBy<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 ? "ThenBy" : "ThenByDescending";
- 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;
- }
- }
- }
|