Barefoot learning LINQ (004): grouping data

Source: Internet
Author: User

Http://u.115.com/file/f27504ff61 demo

You can use the group clause to group results by the specified key. For example, you can specify that results should be grouped by city so that all customers in London or Paris are in their respective groups. In this example, customer. City is the key.

NorthwindDataContext db = new NorthwindDataContext();   var AllCustomers = from Customer in db.Customers                      group Customer by Customer.City;   foreach (var CustomerGroup in AllCustomers)   {       Console.WriteLine("---------------------");       Console.WriteLine("City : {0}", CustomerGroup.Key);       foreach (var Customer in CustomerGroup)       {           Console.WriteLine("Customer Name : {0}", Customer.ContactName);       }   }  NorthwindDataContext db = new NorthwindDataContext();var AllCustomers = from Customer in db.Customers                   group Customer by Customer.City;foreach (var CustomerGroup in AllCustomers){    Console.WriteLine("---------------------");    Console.WriteLine("City : {0}", CustomerGroup.Key);    foreach (var Customer in CustomerGroup)    {        Console.WriteLine("Customer Name : {0}", Customer.ContactName);    }} 

When the Group clause is used to end the query, the results are in the list form. Each element in the list is an object with a key member and a list of elements grouped by the key. You must use a nested foreach loop to generate a group sequence query. The External Loop is used to access each group cyclically, and the internal loop is used to access members of each group cyclically.
If you must reference the results of a group operation, you can use the into keyword to create an identifier that can be further queried. The following query returns only groups of customers with more than two types:

NorthwindDataContext db = new NorthwindDataContext();   var AllCustomers = from Customer in db.Customers                      group Customer by Customer.City into CustomerGroup                      where CustomerGroup.Count() > 2                      orderby CustomerGroup.Key                      select CustomerGroup;  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.