Http://u.115.com/file/f233b821ab demo
It is much easier to start with the strongly typed view of the database. By strongly classifying datacontext objects, you do not need to call gettable. When you use a strongly typed datacontext object, you can use strongly typed tables in all queries.
In the following steps, you create a MERs table as a strongly typed table mapped to the customers table in the database.
Strongly typed datacontext objects
public class Northwind : DataContext { // Table<T> abstracts database details per table/data type. public Table<Customer> Customers; public Table<Order> Orders; public Northwind(string connection) : base(connection) { } } public class Northwind : DataContext{ // Table<T> abstracts database details per table/data type. public Table<Customer> Customers; public Table<Order> Orders; public Northwind(string connection) : base(connection) { }}
Then you can use the following method to use custom strong data context, instead of the built-in datacontext.
// Use a connection string. Northwind db = new Northwind(@"C:\linqtest5\Northwind.mdf"); // Query for customers from Seattle. var SeattleCustomers = from CustomerObject in db.Customers where CustomerObject.City == "Seattle" select CustomerObject; foreach (var CustomerObject in SeattleCustomers) { Console.WriteLine("ID={0}", CustomerObject.CustomerID); } // Freeze the console window. Console.ReadLine();