Documente Academic
Documente Profesional
Documente Cultură
http://msdn.microsoft.com/en-us/library/bb386951
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings. This walkthrough was written by using Visual C# Development Settings.
Prerequisites
You must have completed Walkthrough: Simple Object Model and Query (C#) (LINQ to SQL). This walkthrough builds on that one, including the presence of the northwnd.mdf file in c:\linqtest5.
Overview
This walkthrough consists of three main tasks: Adding an entity class to represent the Orders table in the sample Northwind database. Supplementing annotations to the Customer class to enhance the relationship between the Customer and Order classes. Creating and running a query to test obtaining Order information by using the Customer class.
[Table(Name = "Orders")] public class Order { private int _OrderID = 0; private string _CustomerID; private EntityRef<Customer> _Customer; public Order() { this._Customer = new EntityRef<Customer>(); }
1 of 5
5/25/2012 11:39 AM
http://msdn.microsoft.com/en-us/library/bb386951
[Column(Storage = "_OrderID", DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)] public int OrderID { get { return this._OrderID; } // No need to specify a setter because IsDBGenerated is // true. } [Column(Storage = "_CustomerID", DbType = "NChar(5)")] public string CustomerID { get { return this._CustomerID; } set { this._CustomerID = value; } } [Association(Storage = "_Customer", ThisKey = "CustomerID")] public Customer Customer { get { return this._Customer.Entity; } set { this._Customer.Entity = value; } }
private EntitySet<Order> _Orders; public Customer() { this._Orders = new EntitySet<Order>(); } [Association(Storage = "_Orders", OtherKey = "CustomerID")] public EntitySet<Order> Orders { get { return this._Orders; } set { this._Orders.Assign(value); } }
2 of 5
5/25/2012 11:39 AM
http://msdn.microsoft.com/en-us/library/bb386951
// Query for customers who have placed orders. var custQuery = from cust in Customers where cust.Orders.Any() select cust; foreach (var custObj in custQuery) { Console.WriteLine("ID={0}, Qty={1}", custObj.CustomerID, custObj.Orders.Count); }
Note
You can eliminate the SQL code in the Console window by commenting out db.Log =
Console.Out;.
3. Press Enter in the Console window to stop debugging.
public class Northwind : DataContext { // Table<T> abstracts database details per table/data type. public Table<Customer> Customers; public Table<Order> Orders;
3 of 5
5/25/2012 11:39 AM
http://msdn.microsoft.com/en-us/library/bb386951
2. Modify the Main method to use the strongly typed DataContext as follows:
// Use a connection string. Northwind db = new Northwind(@"C:\linqtest5\northwnd.mdf"); // Query for customers from Seattle. var custQuery = from cust in db.Customers where cust.City == "Seattle" select cust; foreach (var custObj in custQuery) { Console.WriteLine("ID={0}", custObj.CustomerID); } // Freeze the console window. Console.ReadLine();
ID=WHITC
4. Press Enter in the console window to stop debugging.
Next Steps
The next walkthrough (Walkthrough: Manipulating Data (C#) (LINQ to SQL)) demonstrates how to manipulate data. That walkthrough does not require that you save the two walkthroughs in this series that you have already completed.
See Also
Concepts Learning by Walkthroughs (LINQ to SQL)
Yes
No
4 of 5
5/25/2012 11:39 AM
http://msdn.microsoft.com/en-us/library/bb386951
5 of 5
5/25/2012 11:39 AM