Skip to content

Commit 3e7b50f

Browse files
committed
Release Version 0.3
1 parent 4da44b8 commit 3e7b50f

16 files changed

+261
-229
lines changed

mesoft.gridview/Global.asax.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ protected override void Seed(MyDbContext context)
3030
{
3131
var customers = new List<Customer>();
3232
string[] cities = { "Istanbul", "Trabzon", "Ankara", "Izmir", "Samsun", "Erzurum" };
33-
34-
var rnd = new Random(0);
33+
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)};
34+
var rnd = new Random(0);
3535
for (int i = 0; i < 39; i++)
3636
{
3737

@@ -42,7 +42,8 @@ protected override void Seed(MyDbContext context)
4242
Country = "Turkey",
4343
City = cities[rnd.Next(0, cities.Length - 1)],
4444
Phone = "6564811215",
45-
Address = "Address For Company " + i
45+
Address = "Address For Company " + i,
46+
Founded = dates[rnd.Next(0, dates.Length-1)]
4647
};
4748
customers.Add(cust);
4849
}

mesoft.gridview/Migrations/201412290945027_InitialCreate.Designer.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.

mesoft.gridview/Migrations/201412290945027_InitialCreate.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

mesoft.gridview/Migrations/201412290945027_InitialCreate.resx

Lines changed: 0 additions & 126 deletions
This file was deleted.

mesoft.gridview/Migrations/Configuration.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected override void Seed(mesoft.gridview.Models.MyDbContext context)
1919
{
2020
var customers = new List<Customer>();
2121
string[] cities = {"Istanbul", "Trabzon", "Ankara", "Izmir", "Samsun", "Erzurum"};
22-
22+
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) };
2323

2424
for (int i = 0; i < 39; i++)
2525
{
@@ -31,7 +31,8 @@ protected override void Seed(mesoft.gridview.Models.MyDbContext context)
3131
Country = "Turkey",
3232
City = cities[rnd.Next(0, cities.Length-1)],
3333
Phone = "6564811215",
34-
Address = "Address For Company " + i
34+
Address = "Address For Company " + i,
35+
Founded = dates[rnd.Next(0, dates.Length - 1)]
3536
};
3637
customers.Add(cust);
3738
}

mesoft.gridview/Models/Customer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace mesoft.gridview.Models
1+
using System;
2+
3+
namespace mesoft.gridview.Models
24
{
35
public class Customer
46
{
@@ -8,6 +10,7 @@ public class Customer
810
public string Address { get; set; }
911
public string City { get; set; }
1012
public string Country { get; set; }
11-
public string Phone { get; set; }
13+
public string Phone { get; set; }
14+
public DateTime Founded { get; set; }
1215
}
1316
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Reflection;
6+
using System.Web;
7+
using System.Web.Mvc;
8+
9+
namespace mesoft.gridview.Models
10+
{
11+
public static class ExpresssionBuilder
12+
{
13+
private static readonly MethodInfo containsMethod = typeof(string).GetMethod("Contains");
14+
private static readonly MethodInfo startsWithMethod = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
15+
private static readonly MethodInfo endsWithMethod = typeof(string).GetMethod("EndsWith", new Type[] { typeof(string) });
16+
17+
18+
public static Expression<Func<T,bool>> GetExpression<T>(IList<FilterObject> filters)
19+
{
20+
if (filters.Count == 0)
21+
return null;
22+
23+
ParameterExpression param = Expression.Parameter(typeof(T), "t");
24+
Expression exp = null;
25+
26+
if (filters.Count == 1)
27+
exp = GetExpression<T>(param, filters[0]);
28+
else if (filters.Count == 2)
29+
exp = GetExpression<T>(param, filters[0], filters[1]);
30+
else
31+
{
32+
while (filters.Count > 0)
33+
{
34+
var f1 = filters[0];
35+
var f2 = filters[1];
36+
37+
if (exp == null)
38+
exp = GetExpression<T>(param, filters[0], filters[1]);
39+
else
40+
exp = Expression.AndAlso(exp, GetExpression<T>(param, filters[0], filters[1]));
41+
42+
filters.Remove(f1);
43+
filters.Remove(f2);
44+
45+
if (filters.Count == 1)
46+
{
47+
exp = Expression.AndAlso(exp, GetExpression<T>(param, filters[0]));
48+
filters.RemoveAt(0);
49+
}
50+
}
51+
}
52+
53+
return Expression.Lambda<Func<T, bool>>(exp, param);
54+
}
55+
56+
private static Expression GetExpression<T>(ParameterExpression param, FilterObject filter)
57+
{
58+
MemberExpression member = Expression.Property(param, filter.Column);
59+
//ConstantExpression constant = Expression.Constant(filter.Value);
60+
61+
// NEW LOGIC to handle nullable Decimal and DateTime values
62+
UnaryExpression constant = null;
63+
if (member.Type == typeof(Decimal?))
64+
{
65+
constant = Expression.Convert(Expression.Constant(Decimal.Parse(filter.Value)) , member.Type);
66+
}
67+
else if (member.Type == typeof(DateTime?))
68+
{
69+
constant = Expression.Convert(Expression.Constant(DateTime.Parse(filter.Value)), member.Type);
70+
}
71+
else
72+
{
73+
constant = Expression.Convert(Expression.Constant(filter.Value), member.Type);
74+
}
75+
76+
77+
switch (filter.Operator)
78+
{
79+
case FilterOperator.Equals:
80+
return Expression.Equal(member, constant);
81+
82+
case FilterOperator.GreaterThan:
83+
return Expression.GreaterThan(member, constant);
84+
85+
case FilterOperator.GreaterThanOrEqual:
86+
return Expression.GreaterThanOrEqual(member, constant);
87+
88+
case FilterOperator.LessThan:
89+
return Expression.LessThan(member, constant);
90+
91+
case FilterOperator.LessThanOrEqual:
92+
return Expression.LessThanOrEqual(member, constant);
93+
94+
case FilterOperator.Contains:
95+
return Expression.Call(member, containsMethod, constant);
96+
97+
case FilterOperator.StartsWith:
98+
return Expression.Call(member, startsWithMethod, constant);
99+
100+
case FilterOperator.EndsWith:
101+
return Expression.Call(member, endsWithMethod, constant);
102+
103+
case FilterOperator.NotEqual:
104+
return Expression.Negate(Expression.Equal(member, constant));
105+
}
106+
107+
return null;
108+
}
109+
110+
private static BinaryExpression GetExpression<T> (ParameterExpression param, FilterObject filter1, FilterObject filter2)
111+
{
112+
Expression bin1 = GetExpression<T>(param, filter1);
113+
Expression bin2 = GetExpression<T>(param, filter2);
114+
115+
return Expression.AndAlso(bin1, bin2);
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)