C# 将lambda表达式作为方法参数传递

C# 将lambda表达式作为方法参数传递,c#,lambda,expression,C#,Lambda,Expression,我想创建以下方法,该方法接受lambda表达式并通过它对数据进行排序。我似乎无法正确设置这个 看起来像这样的地方???是lambda表达式: public static MyList<T> PageAndSort<T>(this IEnumerable<T> data, ???) 公共静态MyList页面和排序(此IEnumerable数据,?) 将这样使用: MyList.PageAndSort(List<MyEntity> data, x=

我想创建以下方法,该方法接受lambda表达式并通过它对数据进行排序。我似乎无法正确设置这个

看起来像这样的地方???是lambda表达式:

public static MyList<T> PageAndSort<T>(this IEnumerable<T> data, ???)
公共静态MyList页面和排序(此IEnumerable数据,?)
将这样使用:

MyList.PageAndSort(List<MyEntity> data, x=>x.ChildEntity.Name)
MyList.PageAndSort(列表数据,x=>x.ChildEntity.Name)
根据是否需要返回参数,使用
操作
函数

因此:

公共静态MyList页面和排序(此IEnumerable数据,操作排序)

其中,
T
被要排序的类型替换,因此sting等。

LINQ有一个非常类似的方法:。看看它的签名并模仿它:

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector
)
公共静态IORDerenumerable OrderBy(
这是一个数不清的来源,
Func键选择器
)
适用于您的情况:

public static MyList<TSource> PageAndSort<TSource, TKey>(
    this IEnumerable<TSource> data,
    Func<TSource, TKey> keySelector
)
公共静态MyList页面和排序(
这是数不清的数据,
Func键选择器
)

Func
是一个委托,具有一个类型为
T
的参数,该参数返回一个
TResult

标记为c#。。因此我们可以假设:PGrrr,它不允许我添加操作注意:操作后面有一个类型。
Func
没有输入,
Action
也不适合,因为您需要一个可以用作排序键的结果。
public static MyList<TSource> PageAndSort<TSource, TKey>(
    this IEnumerable<TSource> data,
    Func<TSource, TKey> keySelector
)