2012-11-08 36 views
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; 
} 

是否有执行此的任何选项?


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

+0

什么property1和property2的类型? – Heisenbug

回答

0

当你看到它在MSDN你注意到OrderBy,并ThenBy采取Func<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); 
} 
+0

这看起来不错。我认为这正是我想要的。非常感谢你。它有助于很多 – xandi1987

+0

@xandi1987,感激地收到任何投票或接受。 – Jodrell

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

+0

使用文字是不好的做法 – JConstantine

+0

非常感谢我会看看这个 – xandi1987

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

请告诉我你的答案和实际文章??之间的区别。两者都是相同的.. xandi1987使用lamba表达式进行排序..这是很好的。我认为你的文章没有帮助。 –

+0

而不是使用.OrderBy和ThenBy,它将它们放在Linq查询中。我宁愿用这个实现的用户IQueryable,但希望遵循原始的帖子aproach。 –

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

+1

这与@ Davio的答案类似,但您不需要明确指定要订购的财产的类型 – JConstantine

+0

也许有点更多背景:我有不同的“系统”具有不同的视图 - “雇员”和我的对象想要提供一种通用的排序方法。这种方法应该重载与排序相关的属性名称。我想过反思,但我认为/希望会有更好的解决方案 – xandi1987

相关问题