LINQ to SQL Fundamentals
To begin, open Visual Studio, and create a new application named "Simple Linq to SQL" as a console application. Once the IDE is open, click on View, and open the Server Explorer and make a connection to the AdventureWorksLT database, and test that connection.
With that in place, you are ready to create a program that uses LINQ to connect your SQL database. You'll need to include the System.Data.Linq namespace in the references for your project as shown in Figure 15-4 so that the last two using statements will compile.

Figure 15-4. Adding a reference to System.Data.Linq
This will also create the mapping between each class property and the corresponding database column:
public class Customer
{[Column] public string FirstName { get; set; }
[Column] public string LastName { get; set; }
[Column] public string EmailAddress { get; set; }Complete analysis follows Example 15-1.
Example 15-1. Simple LINQ to SQL
using System; using System.Data.Linq; using System.Data.Linq.Mapping; using System.Linq; namespace Simple_Linq_to_SQL { // customer class [Table(Name="SalesLT.Customer")] public class Customer { [Column] public string FirstName { get; set; } [Column] public string LastName { get; set; } [Column] public string EmailAddress { get; set; } // Overrides the Object.ToString( ) to provide a // string representation of the object properties. public override string ToString( ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access