Skip to content

Commit

Permalink
temp commit, adding expression builder to filtering, not finished
Browse files Browse the repository at this point in the history
  • Loading branch information
mesuttalebi committed May 8, 2015
1 parent 4029874 commit 4da44b8
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 7 deletions.
25 changes: 18 additions & 7 deletions mesoft.gridview/Models/GridViewModelProvider.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Helpers;


namespace mesoft.gridview.Models
{
public class GridViewModelProvider
Expand Down Expand Up @@ -46,17 +48,26 @@ private static IQueryable<Customer> GetResources(IQueryable<Customer> Customers,
//filter
if (PagingData.Filters != null)
{
var predicate = PredicateBuilder.True<Customer>();


foreach (var filterObj in PagingData.Filters)
{
switch (filterObj.Column)
{
case "City":
if (filterObj.Value.ToLower() != "all")
customers = customers.Where(x => x.City == filterObj.Value);
break;
var tempValue = filterObj.Value;
var tempColumn = filterObj.Column;
var tempConj = filterObj.Conjunction;
var tempOpr = filterObj.Operator;

}
predicate = predicate.

//switch (filterObj.Column)
//{
// case "City":
// if (filterObj.Value.ToLower() != "all")
// customers = customers.Where(x => x.City == tempValue);

// break;
//}
}
}

Expand Down
103 changes: 103 additions & 0 deletions mesoft.gridview/Models/UniversalPredicateBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**********************************************/
/*By: Pete Montgomery
* https://petemontgomery.wordpress.com/2011/02/10/a-universal-predicatebuilder/
/**********************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace mesoft.gridview.Models
{
/// <summary>
/// Enables the efficient, dynamic composition of query predicates.
/// </summary>
public static class PredicateBuilder
{
/// <summary>
/// Creates a predicate that evaluates to true.
/// </summary>
public static Expression<Func<T, bool>> True<T>() { return param => true; }

/// <summary>
/// Creates a predicate that evaluates to false.
/// </summary>
public static Expression<Func<T, bool>> False<T>() { return param => false; }

/// <summary>
/// Creates a predicate expression from the specified lambda expression.
/// </summary>
public static Expression<Func<T, bool>> Create<T>(Expression<Func<T, bool>> predicate) { return predicate; }

/// <summary>
/// Combines the first predicate with the second using the logical "and".
/// </summary>
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.AndAlso);
}

/// <summary>
/// Combines the first predicate with the second using the logical "or".
/// </summary>
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.OrElse);
}

/// <summary>
/// Negates the predicate.
/// </summary>
public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> expression)
{
var negated = Expression.Not(expression.Body);
return Expression.Lambda<Func<T, bool>>(negated, expression.Parameters);
}

/// <summary>
/// Combines the first expression with the second using the specified merge function.
/// </summary>
static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
{
// zip parameters (map from parameters of second to parameters of first)
var map = first.Parameters
.Select((f, i) => new { f, s = second.Parameters[i] })
.ToDictionary(p => p.s, p => p.f);

// replace parameters in the second lambda expression with the parameters in the first
var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);

// create a merged lambda expression with parameters from the first expression
return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
}

class ParameterRebinder : ExpressionVisitor
{
readonly Dictionary<ParameterExpression, ParameterExpression> map;

ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
{
this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
}

public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
{
return new ParameterRebinder(map).Visit(exp);
}

protected override Expression VisitParameter(ParameterExpression p)
{
ParameterExpression replacement;

if (map.TryGetValue(p, out replacement))
{
p = replacement;
}

return base.VisitParameter(p);
}
}
}
}
1 change: 1 addition & 0 deletions mesoft.gridview/mesoft.gridview.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
<Compile Include="Models\GridviewModel.cs" />
<Compile Include="Models\GridViewModelProvider.cs" />
<Compile Include="Models\MyDbContext.cs" />
<Compile Include="Models\UniversalPredicateBuilder.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit 4da44b8

Please sign in to comment.