' Obtain a list of customers.Dim customers As List(Of Customer) = GetCustomers()
' Return customers that are grouped based on country.Dim countries = From cust In customers
OrderBy cust.Country, cust.City
GroupBy CountryName = cust.Country
Into CustomersInCountry = Group, Count()
OrderBy CountryName
' Output the results.ForEach country In countries
Debug.WriteLine(country.CountryName & " count=" & country.Count)
ForEach customer In country.CustomersInCountry
Debug.WriteLine(" " & customer.CompanyName & " " & customer.City)
NextNext' Output:' Canada count=2' Contoso, Ltd Halifax' Fabrikam, Inc. Vancouver' United States count=1' Margie's Travel Redmond
Dim customers = GetCustomers()
Dim queryResults = From cust In customers
ForEach result In queryResults
Debug.WriteLine(result.CompanyName & " " & result.Country)
Next' Output:' Contoso, Ltd Canada' Margie's Travel United States' Fabrikam, Inc. Canada
クエリを開始するには、From 句または Aggregate 句のいずれかが必要です。 From 句は、クエリのソース コレクションと反復変数を指定します。 次に例を示します。
VB
' Returns the company name for all customers for which' the Country is equal to "Canada".Dim names = From cust In customers
Where cust.Country = "Canada"Select cust.CompanyName
' Returns the company name and ID value for each' customer as a collection of a new anonymous type.Dim customerList = From cust In customers
Select cust.CompanyName, cust.CustomerID
' Returns all product names for which the Category of' the product is "Beverages".Dim names = From product In products
Where product.Category = "Beverages"Select product.Name
' Returns a combined collection of all of the ' processes currently running and a descriptive' name for the process taken from a list of ' descriptive names.Dim processes = From proc In Process.GetProcesses
Join desc In processDescriptions
On proc.ProcessName Equals desc.ProcessName
Select proc.ProcessName, proc.Id, desc.Description
Group By 句
任意。 Group By 句は、クエリ結果の要素をグループ化します。 これを使用して、グループごとに集計関数を適用できます。 次に例を示します。
VB
' Returns a list of orders grouped by the order date' and sorted in ascending order by the order date.Dim orderList = FromorderIn orders
OrderByorder.OrderDate
GroupBy OrderDate = order.OrderDate
Into OrdersByDate = Group
Group Join 句
任意。 Group Join 句は、2 つのコレクションを単一の階層コレクションに結合します。 次に例を示します。
VB
' Returns a combined collection of customers and' customer orders.Dim customerList = From cust In customers
GroupJoin ord In orders On
cust.CustomerID Equals ord.CustomerID
Into CustomerOrders = Group,
TotalOfOrders = Sum(ord.Amount)
Select cust.CompanyName, cust.CustomerID,
CustomerOrders, TotalOfOrders
Aggregate 句
クエリを開始するには、Aggregate 句または From 句のいずれかが必要です。 Aggregate 句は、1 つ以上の集計関数をコレクションに適用します。 たとえば、次の例で行っているように、Aggregate 句を使用して、クエリで返されたすべての要素の合計を計算できます。
VB
' Returns the sum of all order amounts.Dim orderTotal = AggregateorderIn orders
Into Sum(order.Amount)
' Returns the customer company name and largest ' order amount for each customer.Dim customerMax = From cust In customers
AggregateorderIn cust.Orders
Into MaxOrder = Max(order.Amount)
Select cust.CompanyName, MaxOrder
' Returns a list of products with a calculation of' a ten percent discount.Dim discountedProducts = From prod In products
Let Discount = prod.UnitPrice * 0.1Where Discount >= 50Select prod.Name, prod.UnitPrice, Discount
' Returns a list of customers. The first 10 customers' are ignored and the remaining customers are' returned.Dim customerList = From cust In customers
Skip10
Skip While 句
任意。 Skip While 句は、指定された条件が true である限り、コレクションの要素をバイパスし、残りの要素を返します。 次に例を示します。
VB
' Returns a list of customers. The query ignores all' customers until the first customer for whom' IsSubscriber returns false. That customer and all' remaining customers are returned.Dim customerList = From cust In customers
SkipWhile IsSubscriber(cust)
Take 句
任意。 Take 句は、コレクションの先頭から、指定された数の連続する要素を返します。 次に例を示します。
VB
' Returns the first 10 customers.Dim customerList = From cust In customers
Take10
Take While 句
任意。 Take While 句は、指定された条件が true である限り、コレクションの要素を含むようにし、残りの要素をバイパスします。 次に例を示します。
VB
' Returns a list of customers. The query returns' customers until the first customer for whom ' HasOrders returns false. That customer and all ' remaining customers are ignored.Dim customersWithOrders = From cust In customers
OrderBy cust.Orders.Count Descending
TakeWhile HasOrders(cust)
PublicFunction GetAllCustomers() As List(Of Customer)
Dim customers1 = From cust In domesticCustomers
Dim customers2 = From cust In internationalCustomers
Dim customerList = customers1.Union(customers2)
Return customerList.ToList()
EndFunction
Visual Basic における XML の機能には、XML リテラルと XML 軸プロパティがあります。これらを使用して、コード内で XML を簡単に作成、アクセス、照会、および変更できます。 XML リテラルを使用すると、XML をコード内に直接記述できます。 Visual Basic コンパイラは、XML を、最初のクラスのデータ オブジェクトとして処理します。
' Place Imports statements at the top of your program.Imports <xmlns:ns="http://SomeNamespace">
Module Sample1
Sub SampleTransform()
' Create test by using a global XML namespace prefix.Dim contact =
<ns:contact>
<ns:name>Patrick Hines</ns:name>
<ns:phone ns:type="home">206-555-0144</ns:phone>
<ns:phone ns:type="work">425-555-0145</ns:phone>
</ns:contact>
Dim phoneTypes =
<phoneTypes>
<%= From phone In contact.<ns:phone>
Select <type><%= phone.@ns:type %></type>
%>
</phoneTypes>
Console.WriteLine(phoneTypes)
EndSubEndModule