1

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

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

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;
}

有什么选项可以执行此操作吗?


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

4

5 回答 5

3

尝试将排序属性作为从 T 中提取键的 Funcs 传递。

所以:

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);

不知道这是否在语法上完全正确,但它应该为您指明正确的方向。

于 2012-11-08T09:47:16.683 回答
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);

于 2012-11-08T09:48:56.457 回答
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;
}
于 2012-11-08T09:42:53.813 回答
0

当您在 MSDN 上查找它时,您会注意到OrderBy,并将ThenByaFunc<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);
}
于 2012-11-08T09:56:25.923 回答
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");

这里

于 2012-11-08T09:31:45.077 回答