Guest User

Untitled

a guest
Dec 7th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.88 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Collections.Generic;
  7. using System.Reflection;
  8.  
  9. namespace SetupTv
  10. {
  11.   public static class DynamicLinqBuilder
  12.   {
  13.     /*public static IQueryable ApplyNotContainsFilter(IQueryable source, string propertyName, object propertyValue) { }
  14.     public static IQueryable ApplyContainsFilter(IQueryable source, string propertyName, object propertyValue) { }
  15.     public static IQueryable ApplyStartsWithFilter(IQueryable source, string propertyName, object propertyValue) { }    
  16.      */
  17.  
  18.  
  19.  
  20.     private static Expression Contains<T>(Expression searchIn, Expression searchFor)
  21.     {
  22.       return Expression.Call(
  23.           typeof(T).GetMethod("Contains",
  24.               BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
  25.               , searchIn, searchFor);
  26.     }
  27.  
  28.    
  29.     private static Expression<Func<T, bool>> GetContainsExpression<T>(string propertyName, string propertyValue)
  30.     {
  31.       propertyValue = propertyValue.ToUpperInvariant();
  32.       var parameterExp = Expression.Parameter(typeof(T), "type");
  33.       var propertyExp = Expression.Property(parameterExp, propertyName);
  34.       MethodInfo methodToUpperInvariant = typeof(string).GetMethod("ToUpperInvariant");
  35.       var toUpperInvariantMethodExp = Expression.Call(propertyExp, methodToUpperInvariant);
  36.       MethodInfo methodContains = typeof(string).GetMethod("Contains", new[] { typeof(string) });
  37.       var someValue = Expression.Constant(propertyValue, typeof(string));
  38.       var containsMethodExp = Expression.Call(toUpperInvariantMethodExp, methodContains, someValue);
  39.       Expression<Func<T, bool>> containsExpression = Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
  40.       return containsExpression;
  41.     }  
  42.  
  43.  
  44.     public static IQueryable<ProgramDTO> ApplyFilter<T>(IQueryable source, string propertyName, T propertyValue, ConditionOperator conditionOperator)
  45.     {      
  46.       //propertyValue = ChangeType(propertyValue, expression.Type);
  47.  
  48.       LambdaExpression lambdaExpression = null;
  49.       string propertyValueString = propertyValue as string;
  50.       switch (conditionOperator)
  51.       {
  52.         case ConditionOperator.Equals:                    
  53.           BinaryExpression equalMethodExp = null;
  54.           Expression expression;
  55.           ParameterExpression parameterExpression = GetParameterExpression(source, propertyName, propertyValue, out expression);
  56.  
  57.           var parameterExp = new ParameterExpression[1]  
  58.                                        {
  59.                                          parameterExpression
  60.                                        };
  61.           if (propertyValueString != null)
  62.           {
  63.             propertyValueString = propertyValueString.ToUpperInvariant();
  64.  
  65.             MethodInfo methodToUpperInvariant = typeof(string).GetMethod("ToUpperInvariant");
  66.             expression = Expression.Call(expression, methodToUpperInvariant);
  67.           }
  68.  
  69.           lambdaExpression = Expression.Lambda(Expression.Equal(expression, Expression.Constant(propertyValue)), parameterExp);
  70.  
  71.          
  72.            
  73.           break;
  74.  
  75.         case ConditionOperator.Contains:
  76.           if (propertyValueString != null)
  77.           {
  78.             lambdaExpression = GetContainsExpression<ProgramDTO>(propertyName, propertyValueString);
  79.           }          
  80.           break;        
  81.         case ConditionOperator.NotContains:
  82.           break;
  83.         case ConditionOperator.StartsWith:
  84.           break;
  85.       }
  86.      
  87.       MethodCallExpression methodCallExpression = Expression.Call(typeof (Queryable), "Where", new Type[1]
  88.       {
  89.         source.ElementType
  90.       }, new Expression[2]
  91.       {
  92.         source.Expression,
  93.         Expression.Quote(lambdaExpression)
  94.       });
  95.       return source.Provider.CreateQuery<ProgramDTO>(methodCallExpression);
  96.     }
  97.  
  98.    
  99.  
  100.  
  101.     private static ParameterExpression GetParameterExpression<T>(IQueryable source, string propertyName, T value,
  102.                                                                  out Expression expression)
  103.     {
  104.       ParameterExpression parameterExpression = Expression.Parameter(source.ElementType, string.Empty);
  105.       expression = CreatePropertyExpression(parameterExpression, propertyName);
  106.       if (Nullable.GetUnderlyingType(expression.Type) != null && value != null)
  107.         expression = Expression.Convert(expression, RemoveNullableFromType(expression.Type));
  108.       return parameterExpression;
  109.     }
  110.  
  111.     private static Expression CreatePropertyExpression(Expression parameterExpression, string propertyName)
  112.     {
  113.       string str = propertyName;
  114.       var chArray = new char[1]
  115.       {
  116.         '.'
  117.       };
  118.       return str.Split(chArray).Aggregate<string, Expression>(null, (current, propertyOrFieldName) => current != null ? Expression.PropertyOrField(current, propertyOrFieldName) : Expression.PropertyOrField(parameterExpression, propertyOrFieldName));
  119.     }
  120.  
  121.     /*public static T ChangeType<T>(object propertyValue)
  122.     {
  123.       return (T) ChangeType(propertyValue, typeof (T));
  124.     }
  125.  
  126.     public static object ChangeType(object propertyValue, Type type)
  127.     {
  128.       if (type == null)
  129.         throw new ArgumentNullException("type");
  130.       if (propertyValue == null)
  131.       {
  132.         if (TypeAllowsNull(type))
  133.           return (object) null;
  134.         return Convert.ChangeType(propertyValue, type, CultureInfo.CurrentCulture);
  135.       }
  136.       type = RemoveNullableFromType(type);
  137.       if (propertyValue.GetType() == type)
  138.       {
  139.         return propertyValue;
  140.       }
  141.       TypeConverter converter1 = TypeDescriptor.GetConverter(type);
  142.       if (converter1.CanConvertFrom(propertyValue.GetType()))
  143.       {
  144.         return converter1.ConvertFrom(propertyValue);
  145.       }
  146.       TypeConverter converter2 = TypeDescriptor.GetConverter(propertyValue.GetType());
  147.       if (converter2.CanConvertTo(type))
  148.         return converter2.ConvertTo(propertyValue, type);
  149.       throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "CannotConvertType", new object[2]
  150.                                                                                                            {
  151.                                                                                                              propertyValue.GetType(),
  152.                                                                                                              type
  153.                                                                                                            }));
  154.     }
  155.    
  156.     internal static bool TypeAllowsNull(Type type)
  157.     {
  158.       if (!(Nullable.GetUnderlyingType(type) != null))
  159.         return !type.IsValueType;
  160.       return true;
  161.     }
  162. */
  163.     public static Type RemoveNullableFromType(Type type)
  164.     {
  165.       return Nullable.GetUnderlyingType(type) ?? type;
  166.     }
  167.   }
  168. }
Add Comment
Please, Sign In to add comment