204Microsoft Visual Studio 2010: A Beginner’s Guide

Console.ReadKey();

}

}

}

VB:

Module Module1

Sub Main()

Dim myShop As New MyShopDataContext

Dim customers =

From cust In myShop.Customers

Where cust.Name IsNot "Joe"

Select cust

For Each cust In customers

Console.WriteLine("Name: " & cust.Name)

Next

Console.ReadKey()

End Sub

End Module

And here’s the output using my data:

Name: Meg

Name: May

Other than the obvious fact that we’re now getting our data from a real database, the difference between Listing 7-3 and the LINQ to Objects examples you saw earlier are that you have to use the System.Linq namespace (C# only), declare the MyShopDataContext data context, and query Customers from the data context. In C#, the using directive for the System.Linq namespace is required. If you left it out, the compiler will give you the following error message:

“Could not find an implementation of the query pattern for source type ‘System. Data.Linq.Table<LinqToSqlDemoCS.Customer>’. ‘Where’ not found. Are you missing a reference to 'System.Core.dll’ or a using directive for ‘System.Linq’?”

Remember this message because any time you add a new file to a C# project where you are coding LINQ queries, this will be an indication you need to add a using directive for the System.Linq namespace.

Page 227
Image 227
Microsoft 9GD00001 manual Here’s the output using my data