- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一种常见的 GridView 列过滤器方法,可以明智地使用 ColumnName 和 SearchText 过滤 GridView 记录。在这里,当我对可为空的 int 数据列进行操作时,此方法会抛出错误,例如:
Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)'
我的方法代码是:
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;
// this must be of type Expression to accept different type of expressions
// i.e. BinaryExpression, MethodCallExpression, ...
System.Linq.Expressions.Expression body = null;
Expression ex1 = null;
Expression ex2 = null;
switch (colName)
{
case "JobID":
case "status_id":
Int32 _int = Convert.ToInt32(searchText);
searchExpression = Expression.Constant(_int);
containsMethod = typeof(Int32).GetMethod("Equals", new[] { typeof(Int32) });
body = Expression.Call(propertyExpression, containsMethod, searchExpression);
break;
case "group_id":
Int32? _int1 = Convert.ToInt32(searchText);
searchExpression = Expression.Constant(_int1);
containsMethod = typeof(Int32?).GetMethod("Equals", new[] { typeof(Int32?) });
//Error throws from this line
body = Expression.Call(propertyExpression, containsMethod, searchExpression);
break;
case "FileSize":
case "TotalFileSize":
Int64? _int2 = Convert.ToInt64(searchText);
searchExpression = Expression.Constant(_int2);
containsMethod = typeof(Int64?).GetMethod("Equals", new[] { typeof(Int64?) });
body = Expression.Call(propertyExpression, containsMethod, searchExpression);
break;
// section for DateTime? properties
case "PublishDate":
case "Birth_date":
case "Anniversary_date":
case "Profile_Updated_datetime":
case "CompletedOn":
DateTime currentDate = DateTime.ParseExact(searchText, "dd/MM/yyyy", null);
DateTime nextDate = currentDate.AddDays(1);
ex1 = Expression.GreaterThanOrEqual(propertyExpression, Expression.Constant(currentDate, typeof(DateTime?)));
ex2 = Expression.LessThan(propertyExpression, Expression.Constant(nextDate, typeof(DateTime?)));
body = Expression.AndAlso(ex1, ex2);
break;
// section for DateTime properties
case "Created_datetime":
case "Reminder_Date":
case "News_date":
case "thought_date":
case "SubscriptionDateTime":
case "Register_datetime":
case "CreatedOn":
DateTime currentDate1 = DateTime.ParseExact(searchText, "dd/MM/yyyy", null);
DateTime nextDate1 = currentDate1.AddDays(1);
ex1 = Expression.GreaterThanOrEqual(propertyExpression, Expression.Constant(currentDate1));
ex2 = Expression.LessThan(propertyExpression, Expression.Constant(nextDate1));
body = Expression.AndAlso(ex1, ex2);
break;
default:
searchExpression = Expression.Constant(searchText);
containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
body = Expression.Call(propertyExpression, containsMethod, searchExpression);
break;
}
var predicate = Expression.Lambda<Func<T, bool>>(body, new[] { parameter });
return queryable.Where(predicate);
}
else
{
return queryable;
}
}
这是我解雇的查询:
var query = Helper.GetUsers().Where(u => u.Id != user_id).OrderByDescending(u => u.Register_datetime).Select(u => new
{
Id = u.Id,
Name = u.First_name + " " + u.Last_name,
IsActive = u.IsActive,
IsVerified = u.IsVerified,
Username = u.Username,
password = u.password,
Birth_date = u.Birth_date,
Anniversary_date = u.Anniversary_date,
status_id = u.status_id,
group_id = u.group_id,
Profile_Updated_datetime = u.Profile_Updated_datetime,
Register_datetime = u.Register_datetime
}).FilterForColumn(ColumnName, SearchText).ToList();
这里我包含了我的 query.GetType().ToString() 结果,以便更好地理解我对其操作的列的类型。
System.Collections.Generic.List`1[<>f__AnonymousType0`12[System.Int32,System.String,System.Boolean,System.Boolean,System.String,System.String,System.Nullable`1[System.DateTime],System.Nullable`1[System.DateTime],System.Int32,System.Nullable`1[System.Int32],System.Nullable`1[System.DateTime],System.DateTime]]
最佳答案
编辑
在 this question 中找到了解决方案.您需要将表达式转换为 Object
在调用 Equals(object)
之前方法:
var converted = Expression.Convert(searchExpression, typeof(object));
body = Expression.Call(propertyExpression, containsMethod, converted);
Nicodemus13明确设置 searchExpression
的建议的类型为 Object
首先也应该工作。
原创
我还没有发现问题,但我已经使用 Linqpad 在 SSCCE 中重现了这个问题:
void Main()
{
var myInstance = new myClass();
var equalsMethod = typeof(Int32?).GetMethod("Equals", new[] { typeof(Int32?) });
int? nullableInt = 1;
var nullableIntExpr = System.Linq.Expressions.Expression.Constant(nullableInt);
var myInstanceExpr = System.Linq.Expressions.Expression.Constant(myInstance);
var propertyExpr = System.Linq.Expressions.Expression.Property(myInstanceExpr, "MyProperty");
var result = Expression.Call(propertyExpr,equalsMethod,nullableIntExpr); // This line throws the exception.
Console.WriteLine(result);
}
class myClass{public int? MyProperty{get;set;}}
这一行:
containsMethod = typeof(Int32?).GetMethod("Equals", new[] { typeof(Int32?) });
返回 MethodInfo
对于方法 Int32?.Equals (Object other)
.注意参数类型是object
, 不是 Int32
(或 Int32?
)如您所料。
原因是typeof(Int32?)
是System.Nullable<Int32>
,它只有 Equals(object)
方法。
关于c# - 类型 'System.Int32' 的表达式不能用于方法 'System.Object' 的类型 'Boolean Equals(System.Object)' 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23077366/
我有一个带有帮助页面的 Web API 2 项目,该项目在本地运行良好,但当我将其推送到 Azure 时抛出此错误: Method not found: 'System.String System.S
我有两台服务器,但通常运行相同的设置 - IIS、SQL Server 等。一台给我这个错误,另一台没有。我从 Visual Studio 向两者发布相同的代码。 它们都在运行 .NET CLR Ve
System.out声明为 public static final PrintStream out。 但是你可以调用System.setOut()重新分配它。 嗯?如果它是 final,这怎么可能?
System.out被声明为 public static final PrintStream out。 但是您可以调用System.setOut()重新分配它。 嗯?如果是 final,这怎么可能?
我有这个 linq 查询: private void GetReceivedInvoiceTasks(User user, List tasks) { var areaIds = user.A
我有一个 MonoTouch 应用程序,当我为设备编译它时,出现以下错误: Error MT2002: Can not resolve reference: System.Boolean System
您好,我有一个名为 DailyVisitReport 的 View 。在该 View 中,我有两个名为 FromDate 和 toDate 的字段。如果我选择 FromDate 和 ToDate 取决
是否可以从 ObjectContext 对象中读取元组列表? 我在存储过程中有类似这样的数据库查询 SELECT T.Id as Item1, -- this is guid T.Wo
我正在尝试创建 Odata 端点,但每当我尝试执行任何涉及日期的查询时都会收到此错误。 我在下面的非常简单示例中重新创建了它。 数据库表 EDMX(片段)
我正在尝试创建 Odata 端点,但每当我尝试执行任何涉及日期的查询时都会收到此错误。 我在下面的非常简单示例中重新创建了它。 数据库表 EDMX(片段)
我有一个方法可以从数据读取器的数据中生成类类型列表。 if (datareader != null && datareader .HasRows) { Dictionary pDict= GetP
我有一些旧的 C++ 代码,它们使用 stdio 进行输入和输出。该代码还通过 fork 生成新进程。它将 stdio 重新映射到每个新进程,以便每个 session 获取其各自的数据。 我正在考虑使
我的应用程序可以很好地构建/链接/部署到模拟器,但我只是第一次尝试将应用程序构建/部署到真实设备,并且链接器失败。 我不使用 System.Console或 ConsoleColor在我的应用程序的任
主要是我很好奇。 我们有一个名为 Unit 的对象在我们的代码库中 - 代表桥梁或道路的组件。在我们的例子中,看到带有 Unit 的 ReactiveUI 命令可能会模棱两可。作为声明中的泛型之一。
我试图将Object变量转换为StreamWriter。但是,它不起作用。有什么错? StreamWriter file = (StreamWriter) myObject; 最佳答案 myObjec
为什么以下不编译? using System; using System.Linq; using System.Linq.Expressions; public static class Extens
我正在使用 Visual Studio Community 2015 开发面向 .NET 4.5 的 Visual Basic 应用程序.我没有编写应用程序,所以我使用 NuGet 添加了所有缺失的依
我刚刚开始使用 powershell,我正在制作一个非常简单的加密功能。我想获取字符串中的每个字符,将其转换为 int 并添加一个选定的数字,然后将其转换回一个字符。 这工作正常: function
一些使用我的应用程序的人似乎变得越来越 System.MissingMethodException: Method not found: 'System.Object System.Windows.T
我是 C# 和实体的新手 我想知道是否有人在这里帮助我。我选择了哪个返回我的 customerid,所以我想将它作为参数传递给我的构造函数,我的构造函数参数类型是 guid 但我的选择类型不同,我不知
我是一名优秀的程序员,十分优秀!