Note that there are some explanatory texts on larger screens.

plurals
  1. PONullable values on LINQ search of jqGrid
    primarykey
    data
    text
    <p>first of all, sorry for my bad english, isnt my native language.</p> <p>Im using jqgrid on a ASP.NET MVC project on my work, and have some problems when implement search. Im try to use one solution that find on internet using LinqExtensions. The problem ocurres when the entity have nullable value like:</p> <pre><code>public class MyClass() { string StringValue { get; set; } int? IntegerValue { get; set; } } </code></pre> <p>This is because in the database the values accepts null, and need the nullable values on my c# code for other reasons on the project.</p> <p>In another class named LinqExtensions the where clause is like this:</p> <pre><code>public static IQueryable&lt;T&gt; Where&lt;T&gt;(this IQueryable&lt;T&gt; source, string searchProperty, string searchString, string searchOper) { Type type = typeof(T); if (string.IsNullOrEmpty(searchString)) return source; ConstantExpression searchFilter = Expression.Constant(searchString.ToUpper()); ParameterExpression parameter = Expression.Parameter(type, "p"); //PropertyInfo property = type.GetProperty(searchProperty); //Expression propertyAccess = Expression.MakeMemberAccess(parameter, property); MemberExpression memberAccess = null; String[] separador = {"__"}; foreach (var property2 in searchProperty.Split(separador, StringSplitOptions.None)) memberAccess = MemberExpression.Property (memberAccess ?? (parameter as Expression), property2); Expression propertyAccess = memberAccess; if (propertyAccess.Type == typeof(Nullable&lt;DateTime&gt;)) { PropertyInfo valProp = typeof(Nullable&lt;DateTime&gt;).GetProperty("Value"); propertyAccess = Expression.MakeMemberAccess(propertyAccess, valProp); Nullable&lt;DateTime&gt; tn = DateTime.Parse(searchString); searchFilter = Expression.Constant(tn); } //support int? if (propertyAccess.Type == typeof(Nullable&lt;Char&gt;)) { PropertyInfo valProp = typeof(Nullable&lt;Char&gt;).GetProperty("Value"); propertyAccess = Expression.MakeMemberAccess(propertyAccess, valProp); Nullable&lt;Char&gt; tn = Char.Parse(searchString); searchFilter = Expression.Constant(tn); } if (propertyAccess.Type == typeof(Nullable&lt;Int16&gt;)) { PropertyInfo valProp = typeof(Nullable&lt;Int16&gt;).GetProperty("Value"); propertyAccess = Expression.MakeMemberAccess(propertyAccess, valProp); Nullable&lt;Int16&gt; tn = Int16.Parse(searchString); searchFilter = Expression.Constant(tn); } if (propertyAccess.Type == typeof(Nullable&lt;Int32&gt;)) { PropertyInfo valProp = typeof(Nullable&lt;Int32&gt;).GetProperty("Value"); propertyAccess = Expression.MakeMemberAccess(propertyAccess, valProp); Nullable&lt;Int32&gt; tn = Int32.Parse(searchString); searchFilter = Expression.Constant(tn); } if (propertyAccess.Type == typeof(Nullable&lt;Int64&gt;)) { PropertyInfo valProp = typeof(Nullable&lt;Int64&gt;).GetProperty("Value"); propertyAccess = Expression.MakeMemberAccess(propertyAccess, valProp); Nullable&lt;Int64&gt; tn = Int64.Parse(searchString); searchFilter = Expression.Constant(tn); } //support decimal? if (propertyAccess.Type == typeof(Nullable&lt;decimal&gt;)) { PropertyInfo valProp = typeof(Nullable&lt;decimal&gt;).GetProperty("Value"); propertyAccess = Expression.MakeMemberAccess(propertyAccess, valProp); Nullable&lt;decimal&gt; tn = Decimal.Parse(searchString); searchFilter = Expression.Constant(tn); } if (propertyAccess.Type == typeof(Char)) searchFilter = Expression.Constant(Char.Parse(searchString)); if (propertyAccess.Type == typeof(Int16)) searchFilter = Expression.Constant(Int16.Parse(searchString)); if (propertyAccess.Type == typeof(Int32)) searchFilter = Expression.Constant(Int32.Parse(searchString)); if (propertyAccess.Type == typeof(Int64)) searchFilter = Expression.Constant(Int64.Parse(searchString)); if (propertyAccess.Type == typeof(decimal)) searchFilter = Expression.Constant(Decimal.Parse(searchString)); if (propertyAccess.Type == typeof(DateTime)) searchFilter = Expression.Constant(DateTime.Parse(searchString)); MethodInfo startsWith = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }); MethodInfo endsWith = typeof(string).GetMethod("EndsWith", new Type[] { typeof(string) }); MethodInfo contains = typeof(string).GetMethod("Contains", new Type[] { typeof(string) }); //MethodInfo contains = typeof(Int32Extensions).GetMethod("Contains", new Type[] { typeof(Int64), typeof(Int64) }); Expression operation = null; switch (searchOper) { default: case "eq": operation = Expression.Equal(propertyAccess, searchFilter); break; case "ne": operation = Expression.NotEqual(propertyAccess, searchFilter); break; case "lt": operation = Expression.LessThan(propertyAccess, searchFilter); break; case "le": operation = Expression.LessThanOrEqual(propertyAccess, searchFilter); break; case "gt": operation = Expression.GreaterThan(propertyAccess, searchFilter); break; case "ge": operation = Expression.GreaterThanOrEqual(propertyAccess, searchFilter); break; case "bw": operation = Expression.Call(propertyAccess, startsWith, searchFilter); break; case "bn": operation = Expression.Call(propertyAccess, startsWith, searchFilter); operation = Expression.Not(operation); break; case "ew": operation = Expression.Call(propertyAccess, endsWith, searchFilter); break; case "en": operation = Expression.Call(propertyAccess, endsWith, searchFilter); operation = Expression.Not(operation); break; case "cn": operation = Expression.Call(propertyAccess, contains, searchFilter); break; case "nc": operation = Expression.Call(propertyAccess, contains, searchFilter); operation = Expression.Not(operation); break; } var whereExpression = Expression.Lambda(operation, parameter); var resultExpression = Expression.Call(typeof(Queryable), "Where", new Type[] { source.ElementType }, source.Expression, whereExpression); return source.Provider.CreateQuery&lt;T&gt;(resultExpression); } </code></pre> <p>The method in Model class is:</p> <pre><code>public JsonResult GetData(GridSettings grid) { if (Session["SomeValue"] != null) { var query = (new GridModel()).GetQuery(); //Filters if (grid.IsSearch &amp;&amp; grid.Where != null) { //And if (grid.Where.groupOp == "AND") foreach (var rule in grid.Where.rules) query = query.Where&lt;MyClass&gt;(rule.field, rule.data.ToUpper(), rule.op); else { //Or var temp = (new List&lt;MyClass&gt;()).AsQueryable(); foreach (var rule in grid.Where.rules) { var t = query.Where&lt;MyClass&gt;(rule.field, rule.data, rule.op); temp = temp.Concat&lt;MyClass&gt;(t); } //Clean repeat elements query = temp.Distinct&lt;MyClass&gt;(); } } //Order query = query.OrderBy&lt;MyClass&gt;(grid.SortColumn, grid.SortOrder); //Count var count = query.Count(); //Pager var data = query.Skip((grid.PageIndex - 1) * grid.PageSize).Take(grid.PageSize).ToArray(); //Convert var result = new { . . . } } </code></pre> <p>I create the grid, display values very correctly but... when search by string any problem ocurres even if some values are null, and if trying to search by the IntegerValue (that supports null) and Exception throws when some values on entity are null. This problem being transform me on a crazy man.</p> <p>Please, if any have the same problem or know how to solve it, i will be eternally grateful</p> <p>Bye</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload