我有以下代码,我尝试重构它以删除重复的代码。但是,每次我尝试重构它时,我都会卡住。有没有人可以帮助我解决这个问题。
public static IOrderedQueryable<T> ObjectSort<T>(this IQueryable<T> entities, Expression<Func<T, object>> expression, string order = "asc")
{
var sortOrder = order == "asc" ? SortOrder.Ascending : SortOrder.Descending;
return entities.ObjectSort(expression, sortOrder);
}
public static IOrderedQueryable<T> ObjectThenBy<T>(this IOrderedQueryable<T> entities, Expression<Func<T, object>> expression, string order = "asc")
{
var sortOrder = order == "asc" ? SortOrder.Ascending : SortOrder.Descending;
return entities.ObjectThenBy(expression, sortOrder);
}
public static IOrderedQueryable<T> ObjectSort<T>(this IQueryable<T> entities, Expression<Func<T, object>> expression, SortOrder order = SortOrder.Ascending)
{
var unaryExpression = expression.Body as UnaryExpression;
if (unaryExpression != null)
{
var propertyExpression = (MemberExpression)unaryExpression.Operand;
var parameters = expression.Parameters;
if (propertyExpression.Type == typeof(DateTime))
{
var newExpression = Expression.Lambda<Func<T, DateTime>>(propertyExpression, parameters);
return order == SortOrder.Ascending ? entities.OrderBy(newExpression) : entities.OrderByDescending(newExpression);
}
if (propertyExpression.Type == typeof(DateTime?))
{
var newExpression = Expression.Lambda<Func<T, DateTime?>>(propertyExpression, parameters);
return order == SortOrder.Ascending ? entities.OrderBy(newExpression) : entities.OrderByDescending(newExpression);
}
if (propertyExpression.Type == typeof(int))
{
var newExpression = Expression.Lambda<Func<T, int>>(propertyExpression, parameters);
return order == SortOrder.Ascending ? entities.OrderBy(newExpression) : entities.OrderByDescending(newExpression);
}
if (propertyExpression.Type == typeof(int?))
{
var newExpression = Expression.Lambda<Func<T, int?>>(propertyExpression, parameters);
return order == SortOrder.Ascending ? entities.OrderBy(newExpression) : entities.OrderByDescending(newExpression);
}
if (propertyExpression.Type == typeof(bool))
{
var newExpression = Expression.Lambda<Func<T, bool>>(propertyExpression, parameters);
return order == SortOrder.Ascending ? entities.OrderBy(newExpression) : entities.OrderByDescending(newExpression);
}
throw new NotSupportedException("Object type resolution not implemented for this type");
}
return order == SortOrder.Ascending ? entities.OrderBy(expression) : entities.OrderByDescending(expression);
}
public static IOrderedQueryable<T> ObjectThenBy<T>(this IOrderedQueryable<T> entities, Expression<Func<T, object>> expression, SortOrder order = SortOrder.Ascending)
{
var unaryExpression = expression.Body as UnaryExpression;
if (unaryExpression != null)
{
var propertyExpression = (MemberExpression)unaryExpression.Operand;
var parameters = expression.Parameters;
if (propertyExpression.Type == typeof(DateTime))
{
var newExpression = Expression.Lambda<Func<T, DateTime>>(propertyExpression, parameters);
return order == SortOrder.Ascending ? entities.ThenBy(newExpression) : entities.ThenByDescending(newExpression);
}
if (propertyExpression.Type == typeof(DateTime?))
{
var newExpression = Expression.Lambda<Func<T, DateTime?>>(propertyExpression, parameters);
return order == SortOrder.Ascending ? entities.ThenBy(newExpression) : entities.ThenByDescending(newExpression);
}
if (propertyExpression.Type == typeof(int))
{
var newExpression = Expression.Lambda<Func<T, int>>(propertyExpression, parameters);
return order == SortOrder.Ascending ? entities.ThenBy(newExpression) : entities.ThenByDescending(newExpression);
}
if (propertyExpression.Type == typeof(int?))
{
var newExpression = Expression.Lambda<Func<T, int?>>(propertyExpression, parameters);
return order == SortOrder.Ascending ? entities.ThenBy(newExpression) : entities.ThenByDescending(newExpression);
}
if (propertyExpression.Type == typeof(bool))
{
var newExpression = Expression.Lambda<Func<T, bool>>(propertyExpression, parameters);
return order == SortOrder.Ascending ? entities.ThenBy(newExpression) : entities.ThenByDescending(newExpression);
}
throw new NotSupportedException("Object type resolution not implemented for this type");
}
return order == SortOrder.Ascending ? entities.ThenBy(expression) : entities.ThenByDescending(expression);
}
请注意,区别在于 OrderBy 和 ThenBy... 其余部分完全相同。希望有人知道重构它的方法。我试图创建一个 Func 参数来传递 OrderBy 或 ThenBy 方法。但是,我尝试的一切都被卡住了。