Question

i 'm workig for column sorting and there is compile time error:

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":
                searchExpression = Expression.Constant(DateTime.ParseExact(searchText,"dd/MM/yyyy",null));
                containsMethod = typeof(string).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;
        }
    }
}

what that error means. i 'm just wondering what error is just says. please help me what's happening...

----------------------------------Updated----------------------------------------

after changing PublishDate case to :

case "PublishDate":
                    searchExpression = Expression.Constant(DateTime.ParseExact(searchText,"dd/MM/yyyy",null));
                    containsMethod = typeof(string).GetMethod("Equals", new[] { typeof(DateTime) });
                    break;

i face error like this:

Server Error in '/EasyWeb' Application.
Method 'Boolean Equals(System.Object)' 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.Object)' is not defined for type 'System.Nullable`1[System.DateTime]'

Source Error:


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


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

Stack Trace:


[ArgumentException: Method 'Boolean Equals(System.Object)' 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:41
   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
Était-ce utile?

La solution

var containsMethod = <XYZ>;

Tells the compiler - "hey, you, analyze the expression <XYZ> and, whatever type you deduce for that, make that the type for my containsMethod variable".

But in your case, you've not given it any expression to analyze. So you can't use an implicitly typed variable (var), and you have to tell it what type containsMethod is instead.

So:

MethodInfo containsMethod;

maybe.

Autres conseils

From MSDN Implicitly Typed Local Variables:

A local variable declarator in an implicitly typed local variable declaration is subject to the following restrictions:

  • The declarator must include an initializer.

Also this restriction described in C# Specification 8.5.1 Local variable declarations. Local variable declaration looks like:

local-variable-declaration:  
   local-variable-type   local-variable-declarators

local-variable-type:
   type
   var

local-variable-declarators:
   local-variable-declarator
   local-variable-declarators   ,   local-variable-declarator

local-variable-declarator:
   identifier
   identifier   =   local-variable-initializer

local-variable-initializer:
   expression
   array-initializer

Where local variable type is either type name or var. If there is no type named var in scope, then compiler treats this declaration as an implicitly typed local variable declaration. And here comes difference - with named type local variable initializer is optional and declaration can include several variable declarators, i.e. you can write int x; or int x, y;. But for implicitly typed variable there is restrictions defined:

  • The local-variable-declaration cannot include multiple local-variable-declarators.
  • The local-variable-declarator must include a local-variable-initializer.
  • The local-variable-initializer must be an expression.
  • The initializer expression must have a compile-time type.
  • The initializer expression cannot refer to the declared variable itself

I.e. implicitly typed local variable declaration cannot look like var x; or var x, y;.


Thus initializer in your case is not included in variable declaration, then you can use only named type declaration for your variable

MethodInfo containsMethod;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top