![](/img/trans.png)
[英]Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)'
[英]Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)'
我有一個常見的網格視圖列篩選器方法,使用ColumnName和SearchText明智地篩選網格視圖記錄。 這里當我在nullable int數據列上操作時,從這個方法拋出錯誤,如:
類型'System.Int32'的表達式不能用於方法'Boolean Equals(System.Object)'的'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]]
編輯
在這個問題中找到了解決方案。 在調用Equals(object)
方法之前,需要將表達式轉換為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?) });
返回方法Int32?.Equals (Object other)
的MethodInfo
Int32?.Equals (Object other)
。 請注意,參數類型是object
,而不是您期望的Int32
(或Int32?
)。
原因是typeof(Int32?)
是System.Nullable<Int32>
,它只有Equals(object)
方法。
在LinqPad中玩這個,我認為問題在於:
searchExpression = Expression.Constant(_int1);
你打電話的時候:
containsMethod = typeof(Int32?).GetMethod("Equals", new[] { typeof(Int32?) });
你試圖調用的Equals
方法是object.Equals(object)
,編譯器告訴你int?
類型int?
不是該方法所期望的類型object
。
最簡單的修復(雖然我不確定整個代碼是否可行,但這個特殊錯誤會消失)是改變您調用的Expression.Constant
的重載,指定Equals
期望的類型:
searchExpression = Expression.Constant(_int1, typeof(object));
這將編譯 - 但是,有一些事情需要注意。
你原來的Expression.Constant(_int1)
導致一個ConstantExpression
其中Type
int
不是int?
。 如果需要,您需要指定可空類型( Expression.Constant(_int1, typeof(int?))
)。 但是,如上所述,您還需要將其轉換為object
。
指定containsMethod = typeof(Int32?).GetMethod("Equals", new[] { typeof(Int32?) });
無論如何都不應該工作,因為沒有這樣的方法int?.Equals(int?)
, Equals
方法是System.Object
類上的方法的重寫,它接受一個object
參數並且是問題的根。 您也可以使用: typeof(object).GetMethod("Equals", new[] { typeof(object) });
因為這是正確的聲明。
正如我所說,它應該與object
編譯,代碼是否符合你的期望,我不確定,但我想是的。 我期待看到它是否有效:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.