基于方法的查询语法示例:转换运算符 (LINQ to DataSet)

更新:November 2007

本主题中的示例演示如何使用 ToArray<TSource>ToDictionaryToList<TSource> 方法立即执行查询表达式。

这些示例中所使用的 FillDataSet 方法已在 向数据集中加载数据 中指定。

本主题中的示例使用 AdventureWorks 示例数据库中的 Contact、Address、Product、SalesOrderHeader 和 SalesOrderDetail 表。

本主题中的示例使用下面的 using/Imports 语句:

Option Explicit On

Imports System
Imports System.Linq
Imports System.Linq.Expressions
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Globalization
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Globalization;

有关更多信息,请参见如何:在 Visual Studio 中创建 LINQ to DataSet 项目

ToArray

示例

此示例使用 ToArray<TSource> 方法立即将序列计算为数组。

' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)

Dim products As DataTable = ds.Tables("Product")

Dim productsArray = products.AsEnumerable().ToArray()

Dim query = _
        From product In productsArray _
        Select product _
        Order By product.Field(Of Decimal)("ListPrice") Descending

Console.WriteLine("Every price From highest to lowest:")
For Each product In query
    Console.WriteLine(product.Field(Of Decimal)("ListPrice"))
Next
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable products = ds.Tables["Product"];

IEnumerable<DataRow> productsArray = products.AsEnumerable().ToArray();

IEnumerable<DataRow> query =
    from product in productsArray
    orderby product.Field<Decimal>("ListPrice") descending
    select product;

Console.WriteLine("Every price from highest to lowest:");
foreach (DataRow product in query)
{
    Console.WriteLine(product.Field<Decimal>("ListPrice"));
}

ToDictionary

示例

此示例使用 ToDictionary 方法立即将序列和相关键表达式计算为字典。

' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)

Dim products As DataTable = ds.Tables("Product")

Dim scoreRecordsDict = products.AsEnumerable(). _
    ToDictionary(Function(record) record.Field(Of String)("Name"))

Console.WriteLine("Top Tube's ProductID: {0}", _
    scoreRecordsDict("Top Tube")("ProductID"))
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable products = ds.Tables["Product"];


var scoreRecordsDict = products.AsEnumerable().
    ToDictionary(record => record.Field<string>("Name"));
Console.WriteLine("Top Tube's ProductID: {0}",
    scoreRecordsDict["Top Tube"]["ProductID"]);

ToList

示例

此示例使用 ToList<TSource> 方法立即将序列计算为 List<T>,其中 T 的类型为 DataRow

' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)

Dim products As DataTable = ds.Tables("Product")

Dim productList = products.AsEnumerable().ToList()

Dim query = _
    From product In productList _
    Select product _
    Order By product.Field(Of String)("Name")

Console.WriteLine("The sorted name list:")

For Each product In query
    Console.WriteLine(product.Field(Of String)("Name").ToLower(CultureInfo.InvariantCulture))
Next
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable products = ds.Tables["Product"];

IEnumerable<DataRow> productList = products.AsEnumerable().ToList();

IEnumerable<DataRow> query =
    from product in productList
    orderby product.Field<string>("Name")
    select product;

Console.WriteLine("The product list, ordered by product name:");
foreach (DataRow product in query)
{
    Console.WriteLine(product.Field<string>("Name").ToLower(CultureInfo.InvariantCulture));
}

请参见

概念

向数据集中加载数据

标准查询运算符概述

其他资源

LINQ to DataSet 示例