查询表达式语法示例:元素运算符 (LINQ to DataSet)

更新:November 2007

本主题中的示例演示如何使用 FirstElementAt<TSource> 方法以便使用查询表达式语法从 DataSet 中获取 DataRow 元素。

这些示例中所使用的 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 项目

ElementAt

示例

此示例使用 ElementAt<TSource> 方法检索 PostalCode == "M4B 1V7" 的第五个地址。

' 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 addresses As DataTable = ds.Tables("Address")

Dim fifthAddress = ( _
    From address In addresses.AsEnumerable() _
    Where address.Field(Of String)("PostalCode") = "M4B 1V7" _
    Select address.Field(Of String)("AddressLine1")).ElementAt(5)

Console.WriteLine("Fifth address where PostalCode = 'M4B 1V7': " & _
        fifthAddress)
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable addresses = ds.Tables["Address"];

var fifthAddress = (
    from address in addresses.AsEnumerable()
    where address.Field<string>("PostalCode") == "M4B 1V7"
    select address.Field<string>("AddressLine1"))
.ElementAt(5);

Console.WriteLine("Fifth address where PostalCode = 'M4B 1V7': {0}",
    fifthAddress);

First

示例

此示例使用 First 方法返回名字为“Brooke”的第一个联系人。

' 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 contacts As DataTable = ds.Tables("Contact")

Dim query = ( _
    From contact In contacts.AsEnumerable() _
    Where contact.Field(Of String)("FirstName") = "Brooke" _
    Select contact).First()

Console.WriteLine("ContactID: " & query.Field(Of Integer)("ContactID"))
Console.WriteLine("FirstName: " & query.Field(Of String)("FirstName"))
Console.WriteLine("LastName: " & query.Field(Of String)("LastName"))
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable contacts = ds.Tables["Contact"];

DataRow query = (
    from contact in contacts.AsEnumerable()
    where (string)contact["FirstName"] == "Brooke"
    select contact)
    .First();

Console.WriteLine("ContactID: " + query.Field<int>("ContactID"));
Console.WriteLine("FirstName: " + query.Field<string>("FirstName"));
Console.WriteLine("LastName: " + query.Field<string>("LastName"));

请参见

概念

向数据集中加载数据

标准查询运算符概述

其他资源

LINQ to DataSet 示例