06复杂查询(多数据库表)
生活随笔
收集整理的這篇文章主要介紹了
06复杂查询(多数据库表)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1 建立實體類的聯(lián)系
此處使用手動寫代碼實現(xiàn)。
1.1 Order類
private EntityRef<Customer> _Customer;public Order(){this._Customer = new EntityRef<Customer>();}/// <summary>/// 導航到 Customer/// </summary>/// <remarks>/// 使用 CustomerID 關聯(lián)到 Customer ThisKey/// Order的外鍵(CustomerID) 在 Customer 是主鍵,所以使用 ThisKey/// </remarks>[Association(Storage = "_Customer", ThisKey = "CustomerID")]public Customer Customer{get { return this._Customer.Entity; }set { this._Customer.Entity = value; }}注意:定義好實體的聯(lián)系,就可以使用導航性得到Customer和Order,不需要使用Join。
2 建立客戶-訂單聯(lián)系性查詢
// Query for customers who have placed orders.// Any等同于EXISTSvar custQuery2 = Customers.Where(n => n.Orders.Any()); // EXISTSforeach (var custObj in custQuery2){//Console.ReadLine();Console.WriteLine("ID={0}, Qty={1}", custObj.CustomerID,custObj.Orders.Count);}注意:可以不添加聯(lián)系性,建立一方就可導航了。
3 建立強類型的DataContext對象
/// <summary>/// 強類型 Northwind 數(shù)據(jù)庫(元數(shù)據(jù)數(shù)據(jù)庫)/// </summary>/// <remarks>/// 使用強型別 DataContext,不需要調(diào)用 GetTable。/// 數(shù)據(jù)庫可分為:描述數(shù)據(jù)庫和元數(shù)據(jù)數(shù)據(jù)庫。/// 此處是使用元數(shù)據(jù)數(shù)據(jù)庫。/// </remarks>public class Northwind : DataContext{// 強類型tablepublic Table<Customer> Customers;public Table<Order> Orders;public Northwind(string connection) : base(connection) { }}注意:DataContext 的功能
(1)追蹤實體的變動
(2)管理緩存
(3)確保多個數(shù)據(jù)庫實體使用同一個類的實例來表示。
(4)代表相關數(shù)據(jù)庫操作的一組邏輯
轉載于:https://www.cnblogs.com/htht66/archive/2011/12/29/2306812.html
總結
以上是生活随笔為你收集整理的06复杂查询(多数据库表)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA数字处理类使用2
- 下一篇: SQL获取所有用户名,数据库名、所有表名