Use the by contextual keyword in the group clause of a query expression to specify how to group the returned items. For more information, see group clause.
ms.custom: "updateeachrelease"
The C# language reference documents the most recently released version of the C# language. It also contains initial documentation for features in public previews for the upcoming language release.
The documentation identifies any feature first introduced in the last three versions of the language or in current public previews.
The following example shows how to use the by contextual keyword in a group clause to group students by the first letter of each student's last name.
sample · csharpdocs/csharp/language-reference/keywords/snippets/csrefKeywordsContextual.cs10
using System;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace csrefKeywordsContextual
{
class Test
{
class Student
{
public string LastName;
public Student(string name)
{
LastName = name;
}
}
class Category
{
public int ID;
public string Name;
public Category(int i, string n)
{
ID = i;
Name = n;
}
}
class Product
{
public string Name;
public int ProdID;
public int CategoryID;
public Product(string n, int pid, int cid)
{
Name = n;
ProdID = pid;
CategoryID = cid;
}
}
static string[] vegetables = { "carrot", "celery" };
static void Main()
{
List<Student> students = new List<Student>();
//<Snippet10>
var query = from student in students
group student by student.LastName[0];
//</Snippet10>
//<Snippet11>
IEnumerable<string> sortDescendingQuery =
from vegetable in vegetables
orderby vegetable descending
select vegetable;
//</Snippet11>
//<Snippet14>
IEnumerable<string> sortAscendingQuery =
from vegetable in vegetables
orderby vegetable ascending
select vegetable;
//</Snippet14>
List<Category> categories = new List<Category>();
List<Product> products = new List<Product>();
//<Snippet12>
var innerJoinQuery =
from category in categories
join prod in products on category.ID equals prod.CategoryID
select new { ProductName = prod.Name, Category = category.Name };
//</Snippet12>
}
}
}