7.6. Grouping, Sorting, and Making Distinct

LINQ shines when you need to put data into categories or groups. In this example, you use the Group By, Order By, and Distinct keywords to sort and display information about cultures.

7.6.1. Creating the language grouping page

Figure 7-4 shows part of the Web page that you create in this section. The categories (such as el, en, and et) are the two-letter language codes as stored by Windows in the CultureInfo object. The language and countries appear within their categories. For example, Greek (Greece) appears within el and English (Australia) within en. Notice that the query results sort the language codes and countries alphabetically. The page uses two ASP.NET DataList controls, one embedded in the other.

Figure 7-4. Grouping languages and cultures.

To create the grouped and sorted language example, follow these steps:

  1. Add an ASP.NET Web form named languages.aspx to your page.

  2. In Design view, double-click a blank area to create a handler for the Page Load event and then insert the following code inside the handler routine:

    Dim q = _
      From c In _
      System.Globalization.CultureInfo.GetCultures(2) _
      Order By c.EnglishName _
      Order By c.TwoLetterISOLanguageName Distinct _
      Group c By c.TwoLetterISOLanguageName Into Group _
      Select New With _
       {.ISO = TwoLetterISOLanguageName, .culture = Group}
    dlLetters.DataSource = q
    dlLetters.DataBind()
  3. In Source ...

Get ASP.NET 3.5 For Dummies® now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.