以方法為基礎的查詢語法範例:群組
本主題中的範例示範如何使用 GroupBy
方法,透過以方法為基礎的查詢語法來查詢 AdventureWorks Sales 模型。 這些範例中使用的 AdventureWorks Sales Model 是從 AdventureWorks 範例資料庫中的 Contact、Address、Product、SalesOrderHeader 和 SalesOrderDetail 資料表所建立。
此主題中的範例使用下列 using
/Imports
陳述式:
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Globalization;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Data.Common;
Option Explicit On
Option Strict On
Imports System.Data.Objects
Imports System.Globalization
範例 1
以下範例使用 GroupBy
方法傳回依郵遞區號群組的 Address
物件。 這些結果會投影至匿名型別中。
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
var query = context.Addresses
.GroupBy( address => address.PostalCode);
foreach (IGrouping<string, Address> addressGroup in query)
{
Console.WriteLine("Postal Code: {0}", addressGroup.Key);
foreach (Address address in addressGroup)
{
Console.WriteLine("\t" + address.AddressLine1 +
address.AddressLine2);
}
}
}
Using context As New AdventureWorksEntities
Dim query = context.Addresses _
.GroupBy(Function(Address) Address.PostalCode) _
.Select(Function(Address) Address)
For Each addressGroup As IGrouping(Of String, Address) In query
Console.WriteLine("Postal Code: {0}", addressGroup.Key)
For Each address As Address In addressGroup
Console.WriteLine(" " + address.AddressLine1 + address.AddressLine2)
Next
Next
End Using
範例 2
以下範例使用 GroupBy
方法傳回依連絡人姓氏的第一個字母群組的 Contact
物件。 結果也會依連絡人姓氏的第一個字母排序,並且會投影到匿名型別中。
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
var query = context.Contacts
.GroupBy(c => c.LastName.Substring(0,1))
.OrderBy(c => c.Key);
foreach (IGrouping<string, Contact> group in query)
{
Console.WriteLine("Last names that start with the letter '{0}':",
group.Key);
foreach (Contact contact in group)
{
Console.WriteLine(contact.LastName);
}
}
}
Using context As New AdventureWorksEntities
Dim query = context.Contacts _
.GroupBy(Function(c) c.LastName.Substring(0, 1)) _
.OrderBy(Function(c) c.Key) _
.Select(Function(c) c)
For Each group As IGrouping(Of String, Contact) In query
Console.WriteLine("Last names that start with the letter '{0}':", group.Key)
For Each contact As Contact In group
Console.WriteLine(contact.LastName)
Next
Next
End Using
範例 3
以下範例使用 GroupBy
方法傳回客戶 ID 群組的 SalesOrderHeader
物件。 也會傳回每一客戶的銷售數量。
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
var query = context.SalesOrderHeaders
.GroupBy(order => order.CustomerID);
foreach (IGrouping<int, SalesOrderHeader> group in query)
{
Console.WriteLine("Customer ID: {0}", group.Key);
Console.WriteLine("Order count: {0}", group.Count());
foreach (SalesOrderHeader sale in group)
{
Console.WriteLine(" Sale ID: {0}", sale.SalesOrderID);
}
Console.WriteLine("");
}
}
Using context As New AdventureWorksEntities
Dim query = context.SalesOrderHeaders _
.GroupBy(Function(order) order.CustomerID)
' Iterate over each IGrouping
For Each group In query
Console.WriteLine("Customer ID: {0}", group.Key)
Console.WriteLine("Order Count: {0}", group.Count)
For Each sale In group
Console.WriteLine(" Sale ID: {0}", sale.SalesOrderID)
Next
Console.WriteLine("")
Next
End Using