你的空值可以修改属性访问以执行的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)
这是不可能在你的情况下使用? 'OrderBy(x => x.property == null?0:1)' – Pac0