将orderBy或OrderByDescending作为参数传递

时间:2015-07-27 19:37:25

标签: c#

我有这样的方法:

GetUsallyOpeningClosingHour(Func<OpeningDay, TimeSpan> groupByRule)
{
var openingClosingHours = listOfSpecificDayOfWeek.GroupBy(groupByRule).OrderByDescending(x => x.Key);
}

问题是我无法一直坚持OrderByDescending取决于groupByRule参数有时它必须是orderByDescending或OrderBy

我不想依赖这个参数,所以我可以通过另一个参数, 现在我用这种方式调用我的方法:

GetUsallyOpeningClosingHour(x => x.From)

GetUsallyOpeningClosingHour(x => x.To)

我如何传递orderBy类型?

6 个答案:

答案 0 :(得分:6)

最简单的方法是添加一个参数,该参数将指定集合中的顺序。

public void GetUsallyOpeningClosingHour(
    Func<OpeningDay, TimeSpan> groupByRule, 
    bool orderByDesc = false)
{
    var groupedDays = listOfSpecificDayOfWeek.GroupBy(groupByRule);

    var openingClosingHours =
        orderByDesc
            ? groupedDays.OrderByDescending(x => x.Key)
            : groupedDays.OrderBy(x => x.Key);
}

它可以是boolean或自定义enum(我更喜欢枚举,因为它实际上指定了一种排序操作,而boolean指定集合是否应按{{1或不)。

desc

或者您可以提供额外的public enum OrderingType { Ascending, Descending, None } ,它将执行订购操作。但它的签名会很尴尬。

Func

答案 1 :(得分:2)

我猜你可以创建你自己的OrderBy扩展,让你根据参数选择升序/降序。

这样的事情:

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector,
    bool descending
)
{
    return descending ? source.OrderByDescending(keySelector)
                      : source.OrderBy(keySelector);
}

在调用此方法时,您还可以使用枚举而不是布尔值来使事物更具可读性。

答案 2 :(得分:2)

这是OrderByOrderByDescending参数化的最直接方法。幸运的是,Visual Studio可以为您推断出类型;不幸的是这种类型很难写出来。我为static void添加了listOfSpecificDayOfWeek和初始化程序,以便将其粘贴到程序中进行测试。

static void GetUsallyOpeningClosingHour(
  Func<OpeningDay, TimeSpan> groupByRule, 
  Func<IEnumerable<IGrouping<TimeSpan, OpeningDay>>, 
       Func<IGrouping<TimeSpan, OpeningDay>, TimeSpan>,
       IOrderedEnumerable<IGrouping<TimeSpan, OpeningDay>>> order)
{
  IEnumerable<OpeningDay> listOfSpecificDayOfWeek = null;
  var openingClosingHours = order(listOfSpecificDayOfWeek.GroupBy(groupByRule), x => x.Key);
}

您可以像这样调用此函数:

GetUsallyOpeningClosingHour(x => x.From, Enumerable.OrderByDescending);
GetUsallyOpeningClosingHour(x => x.From, Enumerable.OrderBy);

正如其他答案所示,您也可以使用布尔标志来指示升序或降序。

答案 3 :(得分:1)

您必须传入一个参数,因为该方法无法根据参数(例如From / To)知道您要排序的方向。

public [return-type] GetUsallyOpeningClosingHour(Func<OpeningDay, TimeSpan> groupByRule, bool isAscending)
{
    var openingClosingHours = listOfSpecificDayOfWeek.GroupBy(groupByRule);

    if (isAscending)
    {
        openingClosingHours = openingClosingHours.OrderBy(x => x.Key);
    }
    else
    {
        openingClosingHours = openingClosingHours.OrderByDescending(x => x.Key);
    }

    // Return openingClosingHours?  It's not clear how you're using this variable.
}

答案 4 :(得分:1)

        public List<Book> Books(string orderField, bool desc, int skip, int take)
    {
        var propertyInfo = typeof(Book).GetProperty(orderField);

        return _context.Books
            .Where(...)
            .OrderBy(p => !desc ? propertyInfo.GetValue(p, null) : 0)
            .ThenByDescending(p => desc ? propertyInfo.GetValue(p, null) : 0)
            .Skip(skip)
            .Take(take)
            .ToList();
    }

答案 5 :(得分:0)

这是我的代码示例:

public IQueryable<T> GetAllbySearch(
            int pageNumber = 1, int pageSize = 10,
            Dictionary<string, dynamic> filterParams = null,
            Func<IQueryable<T>, IIncludableQueryable<T, object>> include = null,
            bool allIncluded = false
            , Func<IQueryable<T>, IOrderedQueryable<T>> order = null)
        {
            var query = _entity.AsQueryable();

            if (include != null && !allIncluded)
            {
                query = include(query);
            }
            if (allIncluded && include == null)
            {
                foreach (var property in _context.Model.FindEntityType(typeof(T)).GetNavigations()
                    .Where(r => !r.IsCollection()))
                    query = query.Include(property.Name);
            }
            if (filterParams != null && filterParams.Any())
            {
                if (filterParams.Any(r => r.Value != null))
                {
                    var expression = GetSearchFilter(filterParams);
                    if (order != null)
                    {
                        return order(query.Where(expression));
                    }
                    else
                    {
                        return query.Where(expression));
                    }
                }
            }

            if (order != null)
            {
                return order(query);
            }
            else
            {
                return query;
            }

        }