开发者

Is there a C# syntax for a 'null' lambda expression? eg, calling OrderBy(x => x) without the lambda expression? [duplicate]

开发者 https://www.devze.com 2023-04-02 20:06 出处:网络
This question already has answers here: 开发者_StackOverflow中文版 Closed 11 years ago. Possible Duplicate:
This question already has answers here: 开发者_StackOverflow中文版 Closed 11 years ago.

Possible Duplicate:

LINQ identity function?

It seems wasteful to have to type x => x just to sort something like ints or strings... is there a quicker way?


if you have List<T> you can use Sort method: MyList.Sort(), or for other types may be you can find similar functions.

but by Enumerable.OrderBy as MSDN link says, No there isn't anyway.


static void Main()
{
    var array = new[] { 3, 2, 1 };

    var result = array.OrderBy(SimpleSort);

    foreach (var item in result)
    {
        Console.WriteLine(item);
    }
}

public static T SimpleSort<T>(T t)
{
    return t;
}

or create own extension:

public static class Extensions
{
    public static IEnumerable<TSource> OrderBy<TSource>(this IEnumerable<TSource> source)
    {
        return source.OrderBy(t => t);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号