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

FuncsがTからキーを抽出するときに、sortプロパティを渡してみてください。

それで:

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

これを試して、

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 に答える
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で検索すると、に注意してOrderByThenByキー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);
}
于 2012-11-08T09:56:25.923 に答える