Sunday, February 6, 2011

Understand LINQ by reading code (1)

Source
1. Add references:
using System.Data.Linq;
using System.Data.Linq.Mapping;

2. Connect to database:
DataContext db = new DataContext

3. Mapping table and class
[Table(Name = "Customers")]
public class Customer
{
}

4. Query:
Table<Customer> Customers = db.GetTable<Customer>();
IQueryable<Customer> custQuery =
from cust in Customers
where cust.City == "London"
select cust;
foreach (Customer cust in custQuery)
{
Console.WriteLine("ID={0}, City={1}", cust.CustomerID,
cust.City);
}

No comments:

Post a Comment