A.13. Chapter 13

A.13.1.

A.13.1.1.
A.13.1.1.1. Exercise 1 solution

To get the 10 most recent reviews from the system, your query needs two important LINQ constructs: first it needs an Order By (orderby in C#) clause to order the list in descending order. It then needs to use the Take method to take the first 10 reviews from that result set:

VB.NET

Dim recentReviews = (From myReview In myDataContext.Reviews _
                    Order By myReview.CreateDateTime Descending _
                    Select New With {myReview.Title, myReview.Genre.Name}).Take(10)

GridView1.DataSource = recentReviews
GridView1.DataBind()

C#

var recentReviews = (from myReview in myDataContext.Reviews
                    orderby myReview.CreateDateTime descending
                    select new
                    {
                      myReview.Title,
                      myReview.Genre.Name
                    }).Take(10);

GridView1.DataSource = recentReviews;
GridView1.DataBind();

This code also uses the New keyword (new in C#) to create a new, anonymous type that only contains the review's title and the genre's name.

A.13.1.1.2. Exercise 2 solution

The biggest benefit of the ListView control is that it combines the strengths of those other data controls. Just like the GridView control, the ListView control makes it easy to display data in a grid format that users can edit from within the grid. Additionally, the ListView control allows you to insert new records, behavior that is found in controls like DetailsView and FormView but not in GridView.

Finally, the ListView control gives you full control over the markup that gets sent to the browser, an important ...

Get Beginning ASP.NET 3.5: In C# and VB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.