2

I want to create the following method which takes in a lambda expression and orders data by it. I can't seem to correctly set this up.

Would look something like this where ??? is the lambda expression:

public static MyList<T> PageAndSort<T>(this IEnumerable<T> data, ???)

Would be used like this:

MyList.PageAndSort(List<MyEntity> data, x=>x.ChildEntity.Name)
1
  • The Tags say c# .. so we can assume :P Commented Apr 8, 2013 at 10:32

2 Answers 2

6

LINQ has a pretty similar method: OrderBy. Look at its signature and imitate it:

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector
)

Applied to your case:

public static MyList<TSource> PageAndSort<TSource, TKey>(
    this IEnumerable<TSource> data,
    Func<TSource, TKey> keySelector
)

Func<T, TResult> is a delegate with one parameter of type T that returns a TResult.

Sign up to request clarification or add additional context in comments.

Comments

2

Use Action<T> or Func<T> depending of if you require return param or not.

So:

public static MyList<T> PageAndSort<T>(this IEnumerable<T> data, Action<T> sortBy)

where T is replaced by the type you want to sort by, so sting etc.

2 Comments

Grrr, it wont let me add Action<T> note: Action has a < T > type after it.
Func<T> has no input, and Action<T> doesn't fit either, since you need a result that can be used as key for sorting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.