C# 二进制表达式类在比较运算符上引发无效的操作异常

C# 二进制表达式类在比较运算符上引发无效的操作异常,c#,silverlight,linq,invalidoperationexception,C#,Silverlight,Linq,Invalidoperationexception,我正在修改一个开源程序,为Silverlight中的datagrid创建一个通用过滤器。该类的代码如下所示 public PropertyData Property { get; set; } public FilterOperatorType FilterOperator { get; set; } public string FilterValue { get; set; } public Expression GetExpression<T>(

我正在修改一个开源程序,为Silverlight中的datagrid创建一个通用过滤器。该类的代码如下所示

    public PropertyData Property { get; set; }
    public FilterOperatorType FilterOperator { get; set; }
    public string FilterValue { get; set; }

    public Expression GetExpression<T>(ParameterExpression pe)
    {
        if (Property == null || Property.PropertyName == null)
            return null;
        PropertyInfo prop = typeof(T).GetProperty(Property.PropertyName);
        Expression left = Expression.Property(pe, prop);
        Expression right = null;

        switch (prop.PropertyType.Name)
        {
            case "String":
                right = Expression.Constant(FilterValue);
                break;
            case "Int32":
                int val;
                int.TryParse(FilterValue, out val);
                right = Expression.Constant(val);
                break;
            case "Int64":  
                int.TryParse(FilterValue, out val);
                Convert.ToInt32(val);        //does not work
                right = Expression.Constant(val);
                break;
            case "DateTime":
                DateTime dt;
                DateTime.TryParse(FilterValue, out dt);
                right = Expression.Constant(dt);
                break;
        }

        switch (FilterOperator)
        {
            case FilterOperatorType.Equal:
                return Expression.Equal(left, right);
            case FilterOperatorType.GreaterThan:
                return Expression.GreaterThan(left, right);
            case FilterOperatorType.GreaterThanOrEqual:
                return Expression.GreaterThanOrEqual(left, right);
            case FilterOperatorType.LessThan:
                return Expression.LessThan(left, right);
            case FilterOperatorType.LessThanOrEqual:
                return Expression.LessThanOrEqual(left, right);
            case FilterOperatorType.NotEqual:
                return Expression.NotEqual(left, right);
        }
        return null;
    }
}
公共属性数据属性{get;set;}
公共FilterOperator类型FilterOperator{get;set;}
公共字符串FilterValue{get;set;}
公共表达式GetExpression(参数表达式pe)
{
if(Property==null | | Property.PropertyName==null)
返回null;
PropertyInfo prop=typeof(T).GetProperty(Property.PropertyName);
表达式left=Expression.Property(pe,prop);
表达式right=null;
开关(prop.PropertyType.Name)
{
大小写“字符串”:
右=表达式常数(FilterValue);
打破
案例“Int32”:
int-val;
int.TryParse(FilterValue,out val);
右=表达式常数(val);
打破
案例“Int64”:
int.TryParse(FilterValue,out val);
Convert.ToInt32(val);//不起作用
右=表达式常数(val);
打破
案例“日期时间”:
日期时间dt;
DateTime.TryParse(FilterValue,out dt);
右=表达式常数(dt);
打破
}
开关(过滤器操作器)
{
案例过滤器OperatorType.Equal:
返回表达式.Equal(左、右);
案例过滤器OperatorType.GreaterThan:
返回表达式.GreaterThan(左、右);
案例过滤器OperatorType.GreaterThanOrEqual:
返回表达式.GreaterThanOrEqual(左、右);
案例过滤器OperatorType.LessThan:
返回表达式。LessThan(左、右);
案例过滤器OperatorType.LessthanRequire:
返回表达式.lessthanRequal(左、右);
案例过滤器OperatorType.NotEqual:
返回表达式.NotEqual(左、右);
}
返回null;
}
}
每当我尝试使用整数进行筛选时,我都会得到一个InvalidOperationException,该状态为: 未为“System.Int64”和“System.Int32”类型定义二进制运算符Equal


我理解为什么会抛出这个异常,但是在这个代码的示例程序中,我没有得到任何异常,因为用户输入的整数类型是Int32,而在我的应用程序中它是Int64。有人知道如何解决这个问题吗?

你需要将输入解析为
long
而不是
int

我不知道我怎么会错过它。谢谢