Method 'Boolean Equals(System.DateTime)' is not defined for type 'System.Nullable`1[System.DateTime]'

StackOverflow https://stackoverflow.com/questions/22247429

  •  10-06-2023
  •  | 
  •  

Question

i m working with column sorting. here is my code that returns IQueryable result. here i added one Helper class to App_Code side and created one static IQueryable method for column sorting.

public static class Helper
{
    public static IQueryable<T> FilterForColumn<T>(this IQueryable<T> queryable, string colName, string searchText)
    {
        if (colName != null && searchText != null)
        {
            var parameter = Expression.Parameter(typeof(T), "m");
            var propertyExpression = Expression.Property(parameter, colName);
            System.Linq.Expressions.ConstantExpression searchExpression = null;
            System.Reflection.MethodInfo containsMethod = null;
            switch (colName)
            {
                case "Title":
                case "Publisher":
                case "ToUser":
                case "CategoryName":
                case "StatusName":
                case "GroupName":
                case "FileSize":
                    searchExpression = Expression.Constant(searchText);
                    containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
                    break;
                case "PublishDate":
                    DateTime dt = DateTime.ParseExact(searchText, "dd/MM/yyyy", null);
                    searchExpression = Expression.Constant(dt.Date.ToString("MM/dd/yyyy"));
                    containsMethod = typeof(DateTime).GetMethod("Equals", new[] { typeof(DateTime) });
                    break;
            }
            var body = Expression.Call(propertyExpression, containsMethod, searchExpression);
            var predicate = Expression.Lambda<Func<T, bool>>(body, new[] { parameter });
            return queryable.Where(predicate);
        }
        else
        {
            return queryable;
        }
    }
}

but there is error occured from PublishDate section like this :

Server Error in '/EasyWeb' Application.
Method 'Boolean Equals(System.DateTime)' is not defined for type 'System.Nullable`1[System.DateTime]'
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Method 'Boolean Equals(System.DateTime)' is not defined for type 'System.Nullable`1[System.DateTime]'

Source Error:


Line 40:                     break;
Line 41:             }
Line 42:             var body = Expression.Call(propertyExpression, containsMethod, searchExpression);
Line 43:             var predicate = Expression.Lambda<Func<T, bool>>(body, new[] { parameter });
Line 44:             return queryable.Where(predicate);


Source File: f:\EasyWeb\App_Code\Helper.cs    Line: 42

Stack Trace:


[ArgumentException: Method 'Boolean Equals(System.DateTime)' is not defined for type 'System.Nullable`1[System.DateTime]']
   System.Linq.Expressions.Expression.ValidateCallInstanceType(Type instanceType, MethodInfo method) +763804
   System.Linq.Expressions.Expression.ValidateCallArgs(Expression instance, MethodInfo method, ReadOnlyCollection`1& arguments) +71
   System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments) +46
   System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, Expression[] arguments) +31
   Helper.FilterForColumn(IQueryable`1 queryable, String colName, String searchText) in f:\EasyWeb\App_Code\Helper.cs:42
   Admin_Post_History.FillGrid(String CommandName, String ColumnName, String SearchText) in f:\EasyWeb\Admin\Post_History.aspx.cs:63
   Admin_Post_History.btnsearch_Click(Object sender, EventArgs e) in f:\EasyWeb\Admin\Post_History.aspx.cs:2414
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

please help me what am i going wrong here.

Was it helpful?

Solution

It looks like the property you pass in colName is a DateTime? not a DateTime, which means you cannot pass it as a parameter to DateTime.Equals(DateTime).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top