對於我的應用程序,我將一個'Filter'對象傳遞給具有屬性名稱和屬性值的後端。基於屬性字符串名稱與ef進行動態比較
我發現下面的代碼在互聯網上做EF
public static IOrderedQueryable<T> Contains<T>(this IQueryable<T> queryable, string propertyName, string propertyValue)
{
var parameterExp = Expression.Parameter(typeof(T), "type");
var propertyExp = Expression.Property(parameterExp, propertyName);
var method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var someValue = Expression.Constant(propertyValue, typeof(string));
var containsMethodExp = Expression.Call(propertyExp, method, someValue);
var expression = Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
var expcall = Expression.Call(typeof(Queryable), "Where", new[] {typeof(T)}, queryable.Expression,Expression.Quote(expression));
return (IOrderedQueryable<T>)queryable.Provider.CreateQuery<T>(expcall);
}
我稱之爲像這樣的動態包含:
query = query.Contains(filter.Property, (string)filter.Value);
現在我試圖找到一個相同比較要做到這一點:
query = query.Where(x => x.Status == filter.Value);
x.Status是一個枚舉這麼
query = query.Where(x => x.Status.Equals(filter.Value));
拋出
無法創建類型的恆定值 'System.Object的'。在這種情況下,只支持原始類型或枚舉類型
即使當我將它轉換爲int。
所以我需要做的「==」,而不是平等的,但我不知道如何建立一個方法,如包含一個沒有方法名稱,如包含還是equals
我想要什麼,這樣的事情:
query = query.IsTheSame(filter.Property, (Status)filter.Value);
請包括未使用的代碼,並解釋如何你知道它不工作(編譯器錯誤?運行時異常?錯誤的結果?...?) – Richard