Sorting entities
Using LINQ, developers can sort entities within a collection. The following code shows how you can take a collection of
books, order elements by book publisher name and then by title, and select books in an ordered collection. As a result of the
query, you will get an IEnumerable<Book> collection sorted by book publishers and titles.
Hide Copy Code
Book[] books = [Link];
IEnumerable<Book> booksByTitle = from b in books
orderby [Link] descending, [Link]
select b;
foreach (Book book in booksByTitle)
[Link]("Book - {0}\t-\tPublisher: {1} ",
[Link], [Link] );
Alternative code using functions is shown in the following example:
Hide Copy Code
Book[] books = [Link];
IEnumerable<Book> booksByTitle = [Link](book=>[Link])
.ThenBy(book=>[Link]);
foreach (Book book in booksByTitle)
[Link]("Book - {0}\t-\tPublisher: {1} ",
[Link], [Link] );
This type of queries is useful if you have complex structures where you will need to order an entity using the property of a
related entity (in this example, books are ordered by publisher name field which is not placed in the book class at all).
Filtering entities / Restriction