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?
Break Down
Func<TSource,TKey>
: Delegate which will return a key for a given element in the collectionThis 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
Student
String
Func<TSource,TKey>
: This is the passed in lambda expression x => x.Name
I just wonder, what is exactly unclear on MSDN? Here is the topic: http://msdn.microsoft.com/en-us/library/bb548916.aspx
And here are some answers to your questions from that topic:
Type Parameters
TSource - The type of the elements of source.
TKey - The type of the key returned by keySelector.
Parameters
source - A sequence of values to order.
keySelector - A function to extract a key from an element.
comparer - An IComparer to compare keys.
Return Value
An IOrderedEnumerable whose elements are sorted in descending order according to a key.
Also, there are remarks and an example. What you posted here is just a signature of the method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With