diff --git a/mesoft.gridview/Global.asax.cs b/mesoft.gridview/Global.asax.cs index a7e68ea..19affe0 100644 --- a/mesoft.gridview/Global.asax.cs +++ b/mesoft.gridview/Global.asax.cs @@ -30,8 +30,8 @@ protected override void Seed(MyDbContext context) { var customers = new List(); 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++) { @@ -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); } diff --git a/mesoft.gridview/Migrations/201412290945027_InitialCreate.Designer.cs b/mesoft.gridview/Migrations/201412290945027_InitialCreate.Designer.cs deleted file mode 100644 index 83d9f13..0000000 --- a/mesoft.gridview/Migrations/201412290945027_InitialCreate.Designer.cs +++ /dev/null @@ -1,29 +0,0 @@ -// -namespace mesoft.gridview.Migrations -{ - using System.CodeDom.Compiler; - using System.Data.Entity.Migrations; - using System.Data.Entity.Migrations.Infrastructure; - using System.Resources; - - [GeneratedCode("EntityFramework.Migrations", "6.1.2-31219")] - public sealed partial class InitialCreate : IMigrationMetadata - { - private readonly ResourceManager Resources = new ResourceManager(typeof(InitialCreate)); - - string IMigrationMetadata.Id - { - get { return "201412290945027_InitialCreate"; } - } - - string IMigrationMetadata.Source - { - get { return null; } - } - - string IMigrationMetadata.Target - { - get { return Resources.GetString("Target"); } - } - } -} diff --git a/mesoft.gridview/Migrations/201412290945027_InitialCreate.cs b/mesoft.gridview/Migrations/201412290945027_InitialCreate.cs deleted file mode 100644 index fb7218f..0000000 --- a/mesoft.gridview/Migrations/201412290945027_InitialCreate.cs +++ /dev/null @@ -1,32 +0,0 @@ -namespace mesoft.gridview.Migrations -{ - using System; - using System.Data.Entity.Migrations; - - public partial class InitialCreate : DbMigration - { - public override void Up() - { - CreateTable( - "dbo.Customers", - c => new - { - Id = c.Int(nullable: false, identity: true), - CompanyName = c.String(maxLength: 40), - ContactTitle = c.String(maxLength: 40), - Address = c.String(), - City = c.String(), - Country = c.String(), - Phone = c.String(), - }) - .PrimaryKey(t => t.Id); - - } - - public override void Down() - { - DropTable("dbo.Customers"); - } - - } -} diff --git a/mesoft.gridview/Migrations/201412290945027_InitialCreate.resx b/mesoft.gridview/Migrations/201412290945027_InitialCreate.resx deleted file mode 100644 index 8c3f778..0000000 --- a/mesoft.gridview/Migrations/201412290945027_InitialCreate.resx +++ /dev/null @@ -1,126 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - H4sIAAAAAAAEAM1Yy24bNxTdF8g/DLhqAUf0o4vUGCVwJDswGtlGxsmemrmSiHLICclRpW/rop/UX+jlvB96xy4KbTTkvec+5vLwSP/89bf/YRULbwnacCWH5GJwTjyQoYq4nA9Jamdv35EP79/85N9G8cr7VtpdOTv0lGZIFtYm15SacAExM4OYh1oZNbODUMWURYpenp//Ri8uKCAEQSzP87+k0vIYsgd8HCkZQmJTJiYqAmGKddwJMlTvgcVgEhbCkOAXBz7XPFpy+HOQexDvRnCG2QQgZsRjUirLLOZ6/dVAYLWS8yDBBSae1wmg3YwJA0UN17X5oeWcX7pyaO1YQoWpsSo+EvDiqugP7bqf1GVS9Q87eIudtmtXddbFIRllIUATrxvseiS0M9zW5EHpeuZ1DM6q0cAJcp8zb5QKm2oYSkitZuLMe0qngoe/w/pZ/QFyKFMhmoliqrjXWsClJ60S0Hb9BWZF+vcR8Wjbj3YdK7eGT17YvbRXl8R7wOBsKqCag0YTAqs0fAIJmlmInpi1oKXDgKyTveidWCMVJ0yu3UMZFOcPjxPxJmz1GeTcLobkVzw/d3wFUblQ5PFVcjx86GN1CvtDSctC+8ytePVYN1GkwZgdYfDrS9SUtfi1gyjkH/36cZ4WSu56M6dE8Wl9ovvnPBsJjrNbZDBZj6duDVZ2w3lHbiyOvClCtcvJMQOwHerAMaiTyDl3UNPKpmyrvGpqpzm3l3cA3XIJ+BOWJNi4xqVQrHhBfiOM3gbH02ScY9DQbGDLKtsqEnICm0Nn1w1SBHdcGztmlk2Ze3WjKO6ZNd/Clg6XkbqN7hJi3ffSw33Pvfawdher7uQdFhcjvWV1QpVT467ouWZ3MxNMb6DakRJpLLfR9S7vFnk2YVobx+A1GbIN2Nw5HLHiwSZYtXhEZhnPtTLacLnsrq0gsXZZxeLhOAVJNVGKpT6GTzsT0x1P2pvPzlXdHfhdXNE1qaJXnNHhBr84p/tVZO/g5ibEw+YseeQObbA2FuKBMxgE38VIcKy3NpgwyWdgbK5mCPLKu44I/f8IQmpMJA5Uhf+5JOOuq3tFV0+xHa3C5JLpcMF0TxvV0KeKrheC7misEvXnmK1+OTrNho76MaC2VvohrJYeOgLpOM3Tv5QPUzU7RU3OD0MSTRUWkCfakEMnap4+X/m0+dvYH4Ph8xrC/VKWEDoiqEFLm3s5U2XDsbpmRqVJ531MwLIIm3SjLZ/hUON2iAOYydRvTKRochtPIbqXj6lNUntjDMRT0bpefLo7fibs2jn7j4l7Mi9RAqbJsQR4lB9TLqIq77s+S2yDcPNSUA9mhTId4ebrCumhdxduAyraN4YEpCOuZ4gTgWDmUQZsCafkhgr9M8xZuC6vne0g+19Eu+3+mLO5ZrEpMGp/938PdX/4vP8XmYZc3yISAAA= - - - dbo - - \ No newline at end of file diff --git a/mesoft.gridview/Migrations/Configuration.cs b/mesoft.gridview/Migrations/Configuration.cs index 4fc5436..65d1a9b 100644 --- a/mesoft.gridview/Migrations/Configuration.cs +++ b/mesoft.gridview/Migrations/Configuration.cs @@ -19,7 +19,7 @@ protected override void Seed(mesoft.gridview.Models.MyDbContext context) { var customers = new List(); 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++) { @@ -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); } diff --git a/mesoft.gridview/Models/Customer.cs b/mesoft.gridview/Models/Customer.cs index cc77d13..f1ca784 100644 --- a/mesoft.gridview/Models/Customer.cs +++ b/mesoft.gridview/Models/Customer.cs @@ -1,4 +1,6 @@ -namespace mesoft.gridview.Models +using System; + +namespace mesoft.gridview.Models { public class Customer { @@ -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; } } } \ No newline at end of file diff --git a/mesoft.gridview/Models/ExpresssionBuilder.cs b/mesoft.gridview/Models/ExpresssionBuilder.cs new file mode 100644 index 0000000..7b98706 --- /dev/null +++ b/mesoft.gridview/Models/ExpresssionBuilder.cs @@ -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> GetExpression(IList filters) + { + if (filters.Count == 0) + return null; + + ParameterExpression param = Expression.Parameter(typeof(T), "t"); + Expression exp = null; + + if (filters.Count == 1) + exp = GetExpression(param, filters[0]); + else if (filters.Count == 2) + exp = GetExpression(param, filters[0], filters[1]); + else + { + while (filters.Count > 0) + { + var f1 = filters[0]; + var f2 = filters[1]; + + if (exp == null) + exp = GetExpression(param, filters[0], filters[1]); + else + exp = Expression.AndAlso(exp, GetExpression(param, filters[0], filters[1])); + + filters.Remove(f1); + filters.Remove(f2); + + if (filters.Count == 1) + { + exp = Expression.AndAlso(exp, GetExpression(param, filters[0])); + filters.RemoveAt(0); + } + } + } + + return Expression.Lambda>(exp, param); + } + + private static Expression GetExpression(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 (ParameterExpression param, FilterObject filter1, FilterObject filter2) + { + Expression bin1 = GetExpression(param, filter1); + Expression bin2 = GetExpression(param, filter2); + + return Expression.AndAlso(bin1, bin2); + } + } +} \ No newline at end of file diff --git a/mesoft.gridview/Models/GridViewModelProvider.cs b/mesoft.gridview/Models/GridViewModelProvider.cs index 6037060..9582533 100644 --- a/mesoft.gridview/Models/GridViewModelProvider.cs +++ b/mesoft.gridview/Models/GridViewModelProvider.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Dynamic; using System.Linq.Expressions; using System.Web; using System.Web.Helpers; @@ -47,28 +48,19 @@ private static IQueryable GetResources(IQueryable Customers, //filter if (PagingData.Filters != null) - { - var predicate = PredicateBuilder.True(); - - + { foreach (var filterObj in PagingData.Filters) { - 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(Extensions.GetWhereClause(filterObj, typeof(string))); + break; - //switch (filterObj.Column) - //{ - // case "City": - // if (filterObj.Value.ToLower() != "all") - // customers = customers.Where(x => x.City == tempValue); - - // break; - //} - } + //Add Other Filter Columns Here + } + } } @@ -109,6 +101,6 @@ private static IQueryable GetResources(IQueryable Customers, .Skip((PagingData.CurrentPage - 1) * PagingData.ItemsPerPage).Take(PagingData.ItemsPerPage); return customers; - } + } } } \ No newline at end of file diff --git a/mesoft.gridview/Models/GridviewModel.cs b/mesoft.gridview/Models/GridviewModel.cs index ab4dde7..deff171 100644 --- a/mesoft.gridview/Models/GridviewModel.cs +++ b/mesoft.gridview/Models/GridviewModel.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; namespace mesoft.gridview.Models { @@ -34,7 +35,7 @@ public class SortObject public class FilterObject { - public string Column { get; set; } + public string Column { get; set; } public string Value { get; set; } @@ -69,5 +70,107 @@ public enum FilterConjunction { And, Or - } + } + + public class Extensions + { + public static string GetWhereClause(FilterObject filterObj, Type valueType) + { + string whereClause = "true"; + if (valueType != typeof (DateTime)) + { + switch (filterObj.Operator) + { + case FilterOperator.Contains: + if (valueType == typeof (string)) + whereClause += string.Format(" {0} {1}.Contains(\"{2}\")", filterObj.Conjunction, + filterObj.Column, filterObj.Value); + break; + case FilterOperator.GreaterThan: + if (valueType != typeof (string)) + whereClause += string.Format(" {0} {1} > {2}", filterObj.Conjunction, filterObj.Column, + filterObj.Value); + break; + case FilterOperator.GreaterThanOrEqual: + if (valueType != typeof (string)) + whereClause += string.Format(" {0} {1} >= {2}", filterObj.Conjunction, filterObj.Column, + filterObj.Value); + break; + case FilterOperator.LessThan: + if (valueType != typeof (string)) + whereClause += string.Format(" {0} {1} < {2}", filterObj.Conjunction, filterObj.Column, + filterObj.Value); + break; + case FilterOperator.LessThanOrEqual: + if (valueType != typeof (string)) + whereClause += string.Format(" {0} {1} <= {2}", filterObj.Conjunction, filterObj.Column, + filterObj.Value); + break; + case FilterOperator.StartsWith: + if (valueType != typeof (string)) + whereClause += string.Format(" {0} {1}.StartsWith(\"{2}\")", filterObj.Conjunction, + filterObj.Column, filterObj.Value); + break; + case FilterOperator.EndsWith: + if (valueType != typeof (string)) + whereClause += string.Format(" {0} {1}.EndsWith(\"{2}\")", filterObj.Conjunction, + filterObj.Column, filterObj.Value); + break; + case FilterOperator.Equals: + + whereClause += + string.Format(valueType == typeof (string) ? " {0} {1} == \"{2}\"" : " {0} {1} == {2}", + filterObj.Conjunction, filterObj.Column, filterObj.Value); + break; + case FilterOperator.NotEqual: + + whereClause += + string.Format(valueType == typeof (string) ? " {0} {1} != \"{2}\"" : " {0} {1} != {2}", + filterObj.Conjunction, filterObj.Column, filterObj.Value); + break; + default: + throw new ArgumentOutOfRangeException(); + } + } + else + { + DateTime dt; + DateTime.TryParse(filterObj.Value, out dt); + + switch (filterObj.Operator) + { + case FilterOperator.Contains: + break; + case FilterOperator.GreaterThan: + + whereClause += string.Format(" {0} {1} > DateTime(\"{2}\")", filterObj.Conjunction, filterObj.Column, dt); + break; + case FilterOperator.GreaterThanOrEqual: + + whereClause += string.Format(" {0} {1} >= DateTime(\"{2}\")", filterObj.Conjunction, filterObj.Column, dt); + break; + case FilterOperator.LessThan: + + whereClause += string.Format(" {0} {1} < DateTime(\"{2}\")", filterObj.Conjunction, filterObj.Column, dt); + break; + case FilterOperator.LessThanOrEqual: + whereClause += string.Format(" {0} {1} <= DateTime(\"{2}\")", filterObj.Conjunction, filterObj.Column, dt); + break; + case FilterOperator.StartsWith: + break; + case FilterOperator.EndsWith: + break; + case FilterOperator.Equals: + whereClause += string.Format(" {0} {1} == DateTime(\"{2}\")", filterObj.Conjunction, filterObj.Column, dt); + break; + case FilterOperator.NotEqual: + whereClause += string.Format(" {0} {1} != DateTime(\"{2}\")", filterObj.Conjunction, filterObj.Column, dt); + break; + default: + throw new ArgumentOutOfRangeException(); + } + } + return whereClause; + } + } } \ No newline at end of file diff --git a/mesoft.gridview/Scripts/megridview.js b/mesoft.gridview/Scripts/megridview.js index 653af1c..dba93fb 100644 --- a/mesoft.gridview/Scripts/megridview.js +++ b/mesoft.gridview/Scripts/megridview.js @@ -112,7 +112,7 @@ var $column = $(this).data('filter-column'); var $value = $(this).data('filter-value'); var $operator = $(this).data('filter-operator'); - var $conjunction = $(this).data('filter-conjunction'); + var $conjunction = $(this).data('filter-conjunction'); //creating new filter object var filterObj = { Column: $column, Value: $value, Operator: $operator, Conjunction: $conjunction }; diff --git a/mesoft.gridview/Views/Home/_CustomersPartial.cshtml b/mesoft.gridview/Views/Home/_CustomersPartial.cshtml index b5ae0e8..0775dd8 100644 --- a/mesoft.gridview/Views/Home/_CustomersPartial.cshtml +++ b/mesoft.gridview/Views/Home/_CustomersPartial.cshtml @@ -27,11 +27,14 @@ @(Html.DisplayNameFor(x => x.Customers.FirstOrDefault().Phone)) + + @Html.DisplayNameFor(x=> x.Customers.FirstOrDefault().Founded) + - @if (Model.Customers.Count() > 0) + @if (Model.Customers.Any()) { foreach (var c in Model.Customers) { @@ -43,13 +46,14 @@ @c.City @c.Address @c.Phone + @c.Founded } } else { - +
Warning! No Customers Found! diff --git a/mesoft.gridview/Views/Home/_GridViewPartial.cshtml b/mesoft.gridview/Views/Home/_GridViewPartial.cshtml index 379622f..1d6db7b 100644 --- a/mesoft.gridview/Views/Home/_GridViewPartial.cshtml +++ b/mesoft.gridview/Views/Home/_GridViewPartial.cshtml @@ -35,7 +35,7 @@ @foreach (var city in cityFilters) {
  • @city
  • diff --git a/mesoft.gridview/mesoft.gridview.csproj b/mesoft.gridview/mesoft.gridview.csproj index 4d4c6de..67c233a 100644 --- a/mesoft.gridview/mesoft.gridview.csproj +++ b/mesoft.gridview/mesoft.gridview.csproj @@ -59,6 +59,10 @@ + + ..\packages\System.Linq.Dynamic.1.0.4\lib\net40\System.Linq.Dynamic.dll + True + @@ -122,13 +126,10 @@ Global.asax - - - 201412290945027_InitialCreate.cs - + @@ -202,11 +203,6 @@ - - - 201412290945027_InitialCreate.cs - - 10.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) diff --git a/mesoft.gridview/packages.config b/mesoft.gridview/packages.config index 5c49e18..3ee448d 100644 --- a/mesoft.gridview/packages.config +++ b/mesoft.gridview/packages.config @@ -15,5 +15,6 @@ + \ No newline at end of file diff --git a/packages/System.Linq.Dynamic.1.0.4/System.Linq.Dynamic.1.0.4.nupkg b/packages/System.Linq.Dynamic.1.0.4/System.Linq.Dynamic.1.0.4.nupkg new file mode 100644 index 0000000..9e90df5 Binary files /dev/null and b/packages/System.Linq.Dynamic.1.0.4/System.Linq.Dynamic.1.0.4.nupkg differ diff --git a/packages/System.Linq.Dynamic.1.0.4/lib/net40/System.Linq.Dynamic.dll b/packages/System.Linq.Dynamic.1.0.4/lib/net40/System.Linq.Dynamic.dll new file mode 100644 index 0000000..2d142e0 Binary files /dev/null and b/packages/System.Linq.Dynamic.1.0.4/lib/net40/System.Linq.Dynamic.dll differ