Udostępnij za pośrednictwem


Element Operators Method-Based Query Syntax Examples (LINQ to DataSet)

The examples in this topic demonstrate how to use the First method to get DataRow elements from a DataSet using the method query syntax.

The FillDataSet method used in these examples is specified in Loading Data Into a DataSet.

The examples in this topic use the Contact, Address, Product, SalesOrderHeader, and SalesOrderDetail tables in the AdventureWorks sample database.

The examples in this topic use the using/Imports statements found in Method-Based Query Examples (LINQ to DataSet).

For more information, see How to: Create a LINQ to DataSet Project In Visual Studio.

First

Example

This example uses the First method to find the first e-mail address that starts with 'caroline'.

' 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 startsWith As DataRow = contacts.AsEnumerable(). _
    First(Function(contact) contact.Field(Of String) _
    ("EmailAddress").StartsWith("caroline"))

Console.WriteLine("An email address starting with 'caroline': {0}", _
    startsWith.Field(Of String)("EmailAddress"))
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

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

DataRow startsWith = contacts.AsEnumerable().
    First(contact => contact.Field<string>("EmailAddress").StartsWith("caroline"));

Console.WriteLine("An email address starting with 'caroline': {0}",
    startsWith.Field<string>("EmailAddress"));

See Also

Concepts

Loading Data Into a DataSet

Standard Query Operators Overview

Other Resources

LINQ to DataSet Examples