特定属性的通用订单方法

时间:2012-11-08 09:28:16

标签: c#

我是通用编程的新手并且有一个问题:

我正在尝试按特定属性排序列表,这些属性应定义为参数。 请查看代码以更好地理解我想要的内容:

public static IEnumerable<T> SortEmployeesFor<T>(
    IEnumerable<T> list,
    property1,
    property2, 
    OrderOptions options)
{
    switch (options)
    {
        case OrderOptions.1:
            return list.OrderBy(x => property1).ThenBy(x => property2);

        case OrderOptions.2:
            return list.OrderBy(x => property2).ThenBy(x => x.property1);

        ...
    }

    return list;
}

有没有选项可以执行此操作?


P.S。这是我的第一篇文章,如果我做错了,请理解并告诉我。

5 个答案:

答案 0 :(得分:3)

尝试传递排序属性,因为Funcs从T中提取密钥。

所以:

public static IEnumerable<T> SortEmployeesFor<T>(
  IEnumerable<T> list,
  Func<T, TProp1> order1,
  Func<T, TProp2> order2, 
  OrderOptions options)
{
    switch (options)
    {
    case OrderOptions.1:
        return list.OrderBy(order1).ThenBy(order2);

    case OrderOptions.2:
        return list.OrderBy(order2).ThenBy(order1);

    ...
    }

    return list;
}

用法:

SortEmployeesFor<MyType>(
  list, 
  new Func<MyType, typeOfProp1>(x => x.property1), 
  new Func<MyType, typeOfProp2>(x => x.property2), 
  OrderOptions.1);

不知道这是否完全符合语法,但它应该指向正确的方向。

答案 1 :(得分:1)

public static IEnumerable<T> SortEmployeesFor<T>(IEnumerable<T> list, Func<T, IComparable> property1, Func<T, IComparable> property2, OrderOption option)
{
  switch (options)
  {
    case OrderOptions.1:
      return list.OrderBy(property1).ThenBy(property2);

    case OrderOptions.2:
      return list.OrderBy(property2).ThenBy(property1);
  }

  return list;
}

然后使用类似的东西来调用它

list = SortEmployeesFor(list, x => x.Id, y => y.Name, OrderOptions.1);

答案 2 :(得分:0)

试试这个,

list.OrderBy("SomeProperty DESC, SomeOtherProperty ASC");

list.OrderBy("SomeProperty");
list.OrderBy("SomeProperty DESC");
list.OrderBy("SomeProperty DESC, SomeOtherProperty");
list.OrderBy("SomeSubObject.SomeProperty ASC, SomeOtherProperty DESC");

请参阅Here

答案 3 :(得分:0)

你可以用linq做到这一点。

public static IEnumerable<T> SortEmployeesFor<T>(IEnumerable<T> list, OrderOptions options)
{
    switch (options)
    {
        case OrderOptions.1:
            list = from t in list
                     orderby t.property1, t.property2
                     select t;
        case OrderOptions.2:
            list = from t in list
                     orderby t.property2, t.property1
                     select t;        
        .
        .
        .
    }
    return list;
}

答案 4 :(得分:0)

当您在MSDN上查找时,您会注意到OrderByThenByFunc<TSource, TKey>作为键选择器参数。

所以你可以写一些像这样的通用扩展。

public static IEnumerable<T> SortWithOptions<T, TKey1, TKey2>(
    this IEnumerable<T> source,
    Func<T, TKey1> selector1,
    Func<T, TKey2> selector2,
    OrderOptions options)
{
    switch (options)
    {
        case OrderOptions.One:
             return source.OrderBy(selector1).ThenBy(selector2);

        case OrderOptions.Two:
             return source.OrderBy(selector2).ThenBy(selector1);
    }
}

然后,如果您想要为员工编写非通用实现,

public static IEnumerable<Employee> SortEmployees(
    IEnumerable<Employee> unsorted,
    OrderOptions options)
{
    return unsorted.SortWithOptions(
        e => e.Cost,
        e => e.Ability,
        options);
}