Method-Based Query Syntax Examples: Element Operators (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 following using/Imports statements:

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;

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