我有以下代码,我尝试重构此代码以删除重复的代码。然而,每当我尝试重构它时,我都会陷入困境。是否有人可以帮我解决这个问题。
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方法。然而,我尝试的一切都被卡住了。
答案 0 :(得分:1)
OrderBy和ThenBy之间的区别在于OrderBy期望IQueryable,而ThenBy期望IOrderedqueryAble。
现在要使用OrderBy或ThenBy,您需要确定该集合是IQueryable还是IOrderedQueryAble。
之后,如果它是IQueryable,请调用OrderBy。如果它是IOrderedQueryable,则调用ThenBy(在转换为IOrderedQueryable之后。
重构的代码段变为
public static IOrderedQueryable<T> ObjectSortRefactored<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");
}
if(entities.GetType().IsAssignableFrom(typeof(IOrderedQueryable<T>)))
return order == SortOrder.Ascending ? (entities as IOrderedQueryable<T>).ThenBy(expression) : (entities as IOrderedQueryable<T>).ThenByDescending(expression);
return order == SortOrder.Ascending ? entities.OrderBy(expression) : entities.OrderByDescending(expression);
}
我还没有测试过上面的代码片段。