English 中文(简体)
OrderByDescending() per MSDN, what on earth does this mean?
原标题:

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

  • 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
问题回答

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签