Skip to content

Commit

Permalink
Release Version 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
mesuttalebi committed May 12, 2015
1 parent 4da44b8 commit 3e7b50f
Show file tree
Hide file tree
Showing 16 changed files with 261 additions and 229 deletions.
7 changes: 4 additions & 3 deletions mesoft.gridview/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ protected override void Seed(MyDbContext context)
{
var customers = new List<Customer>();
string[] cities = { "Istanbul", "Trabzon", "Ankara", "Izmir", "Samsun", "Erzurum" };

var rnd = new Random(0);
DateTime[] dates = {new DateTime(1982, 5, 2), new DateTime(1983, 3, 5), new DateTime(1988,2,9), new DateTime(1999,12,1),new DateTime(2005,5,15),new DateTime(2010,01,01)};
var rnd = new Random(0);
for (int i = 0; i < 39; i++)
{

Expand All @@ -42,7 +42,8 @@ protected override void Seed(MyDbContext context)
Country = "Turkey",
City = cities[rnd.Next(0, cities.Length - 1)],
Phone = "6564811215",
Address = "Address For Company " + i
Address = "Address For Company " + i,
Founded = dates[rnd.Next(0, dates.Length-1)]
};
customers.Add(cust);
}
Expand Down

This file was deleted.

32 changes: 0 additions & 32 deletions mesoft.gridview/Migrations/201412290945027_InitialCreate.cs

This file was deleted.

126 changes: 0 additions & 126 deletions mesoft.gridview/Migrations/201412290945027_InitialCreate.resx

This file was deleted.

5 changes: 3 additions & 2 deletions mesoft.gridview/Migrations/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected override void Seed(mesoft.gridview.Models.MyDbContext context)
{
var customers = new List<Customer>();
string[] cities = {"Istanbul", "Trabzon", "Ankara", "Izmir", "Samsun", "Erzurum"};

DateTime[] dates = { new DateTime(1982, 5, 2), new DateTime(1983, 3, 5), new DateTime(1988, 2, 9), new DateTime(1999, 12, 1), new DateTime(2005, 5, 15), new DateTime(2010, 01, 01) };

for (int i = 0; i < 39; i++)
{
Expand All @@ -31,7 +31,8 @@ protected override void Seed(mesoft.gridview.Models.MyDbContext context)
Country = "Turkey",
City = cities[rnd.Next(0, cities.Length-1)],
Phone = "6564811215",
Address = "Address For Company " + i
Address = "Address For Company " + i,
Founded = dates[rnd.Next(0, dates.Length - 1)]
};
customers.Add(cust);
}
Expand Down
7 changes: 5 additions & 2 deletions mesoft.gridview/Models/Customer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace mesoft.gridview.Models
using System;

namespace mesoft.gridview.Models
{
public class Customer
{
Expand All @@ -8,6 +10,7 @@ public class Customer
public string Address { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Phone { get; set; }
public DateTime Founded { get; set; }
}
}
118 changes: 118 additions & 0 deletions mesoft.gridview/Models/ExpresssionBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.Mvc;

namespace mesoft.gridview.Models
{
public static class ExpresssionBuilder
{
private static readonly MethodInfo containsMethod = typeof(string).GetMethod("Contains");
private static readonly MethodInfo startsWithMethod = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
private static readonly MethodInfo endsWithMethod = typeof(string).GetMethod("EndsWith", new Type[] { typeof(string) });


public static Expression<Func<T,bool>> GetExpression<T>(IList<FilterObject> filters)
{
if (filters.Count == 0)
return null;

ParameterExpression param = Expression.Parameter(typeof(T), "t");
Expression exp = null;

if (filters.Count == 1)
exp = GetExpression<T>(param, filters[0]);
else if (filters.Count == 2)
exp = GetExpression<T>(param, filters[0], filters[1]);
else
{
while (filters.Count > 0)
{
var f1 = filters[0];
var f2 = filters[1];

if (exp == null)
exp = GetExpression<T>(param, filters[0], filters[1]);
else
exp = Expression.AndAlso(exp, GetExpression<T>(param, filters[0], filters[1]));

filters.Remove(f1);
filters.Remove(f2);

if (filters.Count == 1)
{
exp = Expression.AndAlso(exp, GetExpression<T>(param, filters[0]));
filters.RemoveAt(0);
}
}
}

return Expression.Lambda<Func<T, bool>>(exp, param);
}

private static Expression GetExpression<T>(ParameterExpression param, FilterObject filter)
{
MemberExpression member = Expression.Property(param, filter.Column);
//ConstantExpression constant = Expression.Constant(filter.Value);

// NEW LOGIC to handle nullable Decimal and DateTime values
UnaryExpression constant = null;
if (member.Type == typeof(Decimal?))
{
constant = Expression.Convert(Expression.Constant(Decimal.Parse(filter.Value)) , member.Type);
}
else if (member.Type == typeof(DateTime?))
{
constant = Expression.Convert(Expression.Constant(DateTime.Parse(filter.Value)), member.Type);
}
else
{
constant = Expression.Convert(Expression.Constant(filter.Value), member.Type);
}


switch (filter.Operator)
{
case FilterOperator.Equals:
return Expression.Equal(member, constant);

case FilterOperator.GreaterThan:
return Expression.GreaterThan(member, constant);

case FilterOperator.GreaterThanOrEqual:
return Expression.GreaterThanOrEqual(member, constant);

case FilterOperator.LessThan:
return Expression.LessThan(member, constant);

case FilterOperator.LessThanOrEqual:
return Expression.LessThanOrEqual(member, constant);

case FilterOperator.Contains:
return Expression.Call(member, containsMethod, constant);

case FilterOperator.StartsWith:
return Expression.Call(member, startsWithMethod, constant);

case FilterOperator.EndsWith:
return Expression.Call(member, endsWithMethod, constant);

case FilterOperator.NotEqual:
return Expression.Negate(Expression.Equal(member, constant));
}

return null;
}

private static BinaryExpression GetExpression<T> (ParameterExpression param, FilterObject filter1, FilterObject filter2)
{
Expression bin1 = GetExpression<T>(param, filter1);
Expression bin2 = GetExpression<T>(param, filter2);

return Expression.AndAlso(bin1, bin2);
}
}
}
Loading

0 comments on commit 3e7b50f

Please sign in to comment.