OrderByDescending() per MSDN, what on earth does this mean?
c#, linq, linq-to-sql, msdn
Solution
Break Down
- TSource: This is the type of elements in the collection which need to be ordered
- TKey: The type key by which the elements are ordered.
- `Func<TSource,TKey>`: Delegate which will return a key for a given element in the collection
This function is essentially a sorting function. As such it needs a way to compare the elements in the collection. This particular method assumes that for a given object there is a corresponding key value by which they can be sorted.
Take for example the following class Student
class Student {
string Name { get; set; }
...
}
If I wanted to sort a collection of `Student` instances by their name I could do the following
IEnumerable<Student> col = GetTheStudents();
var ordered = col.OrderByDescending( x => x.Name );
In this case the values would be as follows
- TSource: `Student`
- TKey: `String`
- `Func<TSource,TKey>`: This is the passed in lambda expression `x => x.Name`
Problem
Can someone please help be take apart the elements here and help me understand what they are? ``` public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector ) ``` What is TSource and TKey? What is a keySelector? What the heck is an IOrderedEnumerable? What does Func<> do?? Why is MSDN so cryptic?