Chapter 7: Working with Data

195

Listing 7-2 A program demonstrating how to make a LINQ to objects query

C#:

using System;

using System.Collections.Generic; using System.Linq;

class Customer

{

public string FirstName { get; set; } public string LastName { get; set; }

}

class Program

{

static void Main(string[] args)

{

List<Customer> custList = new List<Customer>

{

new Customer

{

FirstName = "Joe",

LastName = "Zev"

},

new Customer

{

FirstName = "May",

LastName = "Lee"

},

new Customer

{

FirstName = "Meg",

LastName = "Han"

}

};

var customers =

from cust in custList

where cust.FirstName.StartsWith("M") select cust;

foreach (var cust in customers)

{

Console.WriteLine(cust.FirstName);

}

Page 218
Image 218
Microsoft 9GD00001 manual 195