我有一个自定义的IOrderedQueryable函数,如下所示。
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> srcQuery,
string orderColumn, bool isAscending)
{
var type = typeof(T);
var property = type.GetProperty(orderColumn);
if (property == null)
throw new Exception("Column property \"" + orderColumn + "\" does not exist on the type \"" + typeof(T).FullName + "\"");
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExp = Expression.Lambda(propertyAccess, parameter);
MethodCallExpression resultExp =
Expression.Call(typeof(Queryable),isAscending ? "OrderBy" :
"OrderByDescending", new Type[] { type, property.PropertyType },
srcQuery.Expression, Expression.Quote(orderByExp));
return (IOrderedQueryable<T>)srcQuery.Provider.CreateQuery<T>(resultExp);
}
我想知道无论如何我都可以使用condition属性执行命令!= null以显示非空值,然后在上面的orderByExp表达式中显示空值
答案 0 :(得分:2)
您可以修改属性访问权限以执行相当于query.OrderBy(x => x.property == null ? 0 : 1)
public static IOrderedQueryable<T> OrderByNull<T>(IQueryable<T> srcQuery, string orderColumn, bool isAscending)
{
var type = typeof(T);
var property = type.GetProperty(orderColumn);
if (property == null)
throw new Exception("Column property \"" + orderColumn + "\" does not exist on the type \"" + typeof(T).FullName + "\"");
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.Condition(
Expression.Equal(Expression.MakeMemberAccess(parameter, property), Expression.Constant(null, property.PropertyType)),
Expression.Constant(1),
Expression.Constant(0));
var orderByExp = Expression.Lambda(propertyAccess, parameter);
MethodCallExpression resultExp =Expression.Call( typeof(Queryable), isAscending ? "OrderBy" : "OrderByDescending", new Type[] { type, typeof(int) },
srcQuery.Expression, Expression.Quote(orderByExp));
return (IOrderedQueryable<T>)srcQuery.Provider.CreateQuery<T>(resultExp);
}
注意:如果属性不可为空(例如int?
或double?
),则会发生错误,
修改强>:
以上版本仅适用于可空属性。另一个版本是按可空列的默认值排序,并为所有其他列表进行正常排序:if (property is nullable) query.OrderBy(x => x.property == null ? default(basePropertyType) : x.property) else query.OrderBy(x => x.property)
该版本的版本如下:
static Dictionary<Type, object> DefaultTypeValues = new Dictionary<Type, object>
{
{ typeof(string), "" },
// { typeof(DateTime?), new DateTime(1753,1,1) } // Min date for sql date
{ typeof(DateTime?), new DateTime(9999,12,31} // Max date for sql date
};
public static object GetDefaultValue(Type t)
{
object defaultValue;
if(!DefaultTypeValues.TryGetValue(t, out defaultValue))
{
if(t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>))
{
defaultValue = Activator.CreateInstance(t.GetGenericArguments().Single());
}
else
{
throw new NotSupportedException("Could not get default value for type " + t.FullName + " consider adding it in DefaultTypeValues");
}
}
return defaultValue;
}
public static IOrderedQueryable<T> OrderBy<T>(IQueryable<T> srcQuery, string orderColumn, bool isAscending)
{
var type = typeof(T);
var property = type.GetProperty(orderColumn);
if (property == null)
throw new Exception("Column property \"" + orderColumn + "\" does not exist on the type \"" + typeof(T).FullName + "\"");
var parameter = Expression.Parameter(type, "p");
// default sort is performed by o=> o.Prop
Expression propertyAccess = Expression.MakeMemberAccess(parameter, property);
var propType = property.PropertyType;
// If property is nullable we add teh null check
if (propType == typeof(string) || (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
var defaultValue = GetDefaultValue(propType);
// If the property is nullable we sort by (o => o.Prop == null ? default(propType) : o.Prop)
propertyAccess = Expression.Condition(
Expression.Equal(propertyAccess, Expression.Constant(null, propType)),
Expression.Constant(defaultValue, propType),
propertyAccess
);
}
var orderByExp = Expression.Lambda(propertyAccess, parameter);
MethodCallExpression resultExp = Expression.Call(typeof(Queryable), isAscending ? "OrderBy" : "OrderByDescending", new Type[] { type, propType },
srcQuery.Expression, Expression.Quote(orderByExp));
return (IOrderedQueryable<T>)srcQuery.Provider.CreateQuery<T>(resultExp);
}
默认值可以通过DefaultTypeValues中的类型进行自定义,或者如果未指定任何值,则默认值将默认为基础可空类型(例如,int?
将为0)