For example, the following defines a simple schema based on the Person.Contact table from the
AdventureWorks database:
[Table(Name="Person.Contact")]
public class Contact
{
[Column(DBType = "nvarchar(50) not null")]
public string FirstName;
[Column(DBType = "nvarchar(50) not null")]
public string LastName;
[Column(DBType = "nvarchar(50) not null")]
public string EmailAddress;
}
14
Chapter 1: Project LINQ
Once this schema is defined, a query can be issued. This is where LINQ comes in. Using the standard
query operators, LINQ translates the query from its original query expression form into a SQL query for
execution on the server:
private void button5_Click(object sender, EventArgs e)
{
DataContext context = new DataContext("Initial
Catalog=AdventureWorks;Integrated
Security=sspi");
Table
contact = context.GetTable();
var query =
from c in contact
select new { c.FirstName, c.LastName, c.EmailAddress} ;
foreach (var item in query)
listBox1.Items.Add(item.FirstName + " " + item.LastName + " " +
item.EmailAddress);
}
Following are partial results from the query:
gustavo Achong gustavo0@adventure-works.
Pages:
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56