Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Reflection;
- using System.Data.Linq.SqlClient;
- namespace Core.Linq {
- //[DebuggerStepThrough]
- public static class EnumerableExtentions {
- //Func<TSource,bool> predicate
- public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source,TSource valueIfDefault) {
- TSource value = source.FirstOrDefault<TSource>();
- if(value == null)
- return valueIfDefault;
- else
- return value;
- }
- public static IEnumerable<TSource> Match<TSource>(this IEnumerable<TSource> first,IEnumerable<TSource> second) {
- List<TSource> matchedList = new List<TSource>();
- foreach(TSource item in second) {
- if(first.Contains<TSource>(item))
- matchedList.Add(item);
- }
- return matchedList.AsEnumerable();
- }
- public static IEnumerable<T> IIf<T>(this IEnumerable<T> source,Func<T,bool> predicate,T resultIfFalse) {
- var matched = source.Where(predicate);
- if(matched.Count() > 0)
- return matched;
- else {
- List<T> result = new List<T>();
- result.Add(resultIfFalse);
- return result.AsEnumerable();
- }
- }
- public static IEnumerable<object> IIf<T>(this IEnumerable<T> source,Func<T,bool> predicate,Func<T,object> selectorIfFalse) {
- var matched = source.Where(predicate);
- if(matched.Count() > 0)
- return matched.Cast<object>().AsEnumerable();
- else {
- return source.Select(selectorIfFalse);
- }
- }
- public static IEnumerable<T> Random<T>(this IEnumerable<T> source) {
- return Random<T>(source,Environment.TickCount);
- }
- public static IEnumerable<T> Random<T>(this IEnumerable<T> source,int seed) {
- Random rnd = new Random(seed);
- T[] arr = source.ToArray();
- T[] arrto = new T[arr.Length];
- int sourceLength = arr.Length;
- for(int i = 0;i < sourceLength;i++) {
- arrto[i] = arr[rnd.Next(sourceLength - 1)];
- }
- return arrto;
- }
- public static IEnumerable<T> Each<T>(this IEnumerable<T> source,Action<T> fEachItem) {
- foreach(var item in source)
- fEachItem(item);
- return source;
- }
- public static IEnumerable<T> Edit<T>(this IEnumerable<T> source,Func<T,T> fEdit) {
- T[] sourceArray = source.ToArray();
- for(int i = 0;i < sourceArray.Length;i++) {
- sourceArray[i] = fEdit(sourceArray[i]);
- }
- return sourceArray.AsEnumerable();
- }
- public static IQueryable<T> Like<T>(this IQueryable<T> source,string propertyName,string keyword) {
- var type = typeof(T);
- var property = type.GetProperty(propertyName);
- var parameter = Expression.Parameter(type,"p");
- var propertyAccess = Expression.MakeMemberAccess(parameter,property);
- var constant = Expression.Constant("%" + keyword + "%");
- MethodCallExpression methodExp = Expression.Call(null,typeof(SqlMethods).GetMethod("Like",new Type[] { typeof(string),typeof(string) }),propertyAccess,constant);
- Expression<Func<T,bool>> lambda = Expression.Lambda<Func<T,bool>>(methodExp,parameter);
- return source.Where(lambda);
- }
- public static IQueryable<T> Where<T>(this IQueryable<T> source,string predicate,params object[] values) {
- if(source == null)
- throw new ArgumentNullException("source");
- if(predicate == null)
- throw new ArgumentNullException("predicate");
- LambdaExpression lambda = System.Linq.Dynamic.DynamicExpression.ParseLambda(source.ElementType,typeof(bool),predicate,values);
- return source.Provider.CreateQuery<T>(
- Expression.Call(
- typeof(Queryable),"Where",
- new Type[] { source.ElementType },
- source.Expression,Expression.Quote(lambda)));
- }
- public static IQueryable<T> OrderByt<T>(this IQueryable<T> source,string propertyName,bool asc) {
- var type = typeof(T);
- string methodName = asc ? "OrderBy" : "OrderByDescending";
- var property = type.GetProperty(propertyName);
- var parameter = Expression.Parameter(type,"p");
- var propertyAccess = Expression.MakeMemberAccess(parameter,property);
- var orderByExp = Expression.Lambda(propertyAccess,parameter);
- MethodCallExpression resultExp = Expression.Call(typeof(Queryable),methodName,new Type[] { type,property.PropertyType },source.Expression,Expression.Quote(orderByExp));
- return source.Provider.CreateQuery<T>(resultExp);
- }
- #region OrderBy & OrderByName
- public static IOrderedQueryable<TSource> OrderByName<TSource>(this IQueryable<TSource> source,string propertyName) {
- return OrderBy(source,propertyName);
- }
- public static IOrderedQueryable<TSource> OrderByDescendingName<TSource>(this IQueryable<TSource> source,string propertyName) {
- return OrderByDescending(source,propertyName);
- }
- public static IOrderedQueryable<TSource> OrderByName<TSource>(this IQueryable<TSource> source,string propertyName,bool ascending) {
- if(ascending)
- return OrderByName(source,propertyName);
- else
- return OrderByDescendingName(source,propertyName);
- }
- public static IQueryable<TSource> OrderByName<TSource>(this IEnumerable<TSource> source,string propertyName) {
- return OrderBy(source.AsQueryable(),propertyName);
- }
- public static IOrderedQueryable<TSource> OrderByDescendingName<TSource>(this IEnumerable<TSource> source,string propertyName) {
- return OrderByDescending(source.AsQueryable(),propertyName);
- }
- public static IOrderedQueryable<TSource> OrderByName<TSource>(this IEnumerable<TSource> source,string propertyName,bool ascending) {
- if(ascending)
- return OrderByName(source.AsQueryable(),propertyName);
- else
- return OrderByDescendingName(source.AsQueryable(),propertyName);
- }
- public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source,string property) {
- return ApplyOrder<T>(source,property,"OrderBy");
- }
- public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source,string property) {
- return ApplyOrder<T>(source,property,"OrderByDescending");
- }
- public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source,string property) {
- return ApplyOrder<T>(source,property,"ThenBy");
- }
- public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source,string property) {
- return ApplyOrder<T>(source,property,"ThenByDescending");
- }
- static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source,string property,string methodName) {
- string[] props = property.Split('.');
- Type type = typeof(T);
- ParameterExpression arg = Expression.Parameter(type,"x");
- Expression expr = arg;
- foreach(string prop in props) {
- // use reflection (not ComponentModel) to mirror LINQ
- PropertyInfo pi = type.GetProperty(prop);
- if(pi != null) {
- expr = Expression.Property(expr,pi);
- type = pi.PropertyType;
- }
- }
- Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T),type);
- LambdaExpression lambda = Expression.Lambda(delegateType,expr,arg);
- object result = typeof(Queryable).GetMethods().Single(
- method => method.Name == methodName
- && method.IsGenericMethodDefinition
- && method.GetGenericArguments().Length == 2
- && method.GetParameters().Length == 2)
- .MakeGenericMethod(typeof(T),type)
- .Invoke(null,new object[] { source,lambda });
- return (IOrderedQueryable<T>)result;
- }
- #endregion
- #region Between
- public static IEnumerable<T> Between<T>(this IEnumerable<T> source,Func<T,bool> endPredicate) {
- if(source == null)
- throw Throw.ArgumentNull("source");
- if(endPredicate == null)
- throw Throw.ArgumentNull("endPredicate");
- return BetweenIterator<T>(source,b => true,endPredicate);
- }
- public static IEnumerable<T> Between<T>(this IEnumerable<T> source,Func<T,bool> startPredicate,Func<T,bool> endPredicate) {
- if(source == null)
- throw Throw.ArgumentNull("source");
- if(startPredicate == null)
- throw Throw.ArgumentNull("startPredicate");
- if(endPredicate == null)
- throw Throw.ArgumentNull("endPredicate");
- return BetweenIterator<T>(source,startPredicate,endPredicate);
- }
- public static IEnumerable<T> Between<T>(this IEnumerable<T> source,T start,T end) where T:IComparable {
- bool includeStartEnd = false;
- return Between<T>(source,start,end,includeStartEnd);
- }
- public static IEnumerable<T> Between<T>(this IEnumerable<T> source,T start,T end,bool includeStartEnd) where T:IComparable {
- if(source == null)
- throw Throw.ArgumentNull("source");
- if(start == null)
- throw Throw.ArgumentNull("start");
- if(end == null)
- throw Throw.ArgumentNull("end");
- bool foundStart = false;
- bool foundEnd = false;
- foreach(T element in source) {
- if(foundEnd) {
- yield break;
- }
- else {
- if(element.Equals(start)) {
- foundStart = true;
- if(includeStartEnd)
- yield return element;
- else
- continue;
- }
- else {
- if(foundStart) {
- if(element.Equals(end)) {
- foundEnd = true;
- if(includeStartEnd)
- yield return element;
- else
- continue;
- }
- else
- yield return element;
- }
- }
- }
- }
- }
- static IEnumerable<T> BetweenIterator<T>(IEnumerable<T> source,Func<T,bool> startPredicate,Func<T,bool> endPredicate) {
- bool foundStart = false;
- foreach(T element in source) {
- if(startPredicate(element))
- foundStart = true;
- if(foundStart)
- if(!endPredicate(element))
- yield return element;
- else
- yield break;
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment