1. 程式人生 > >ObjectQuery查詢及方法

ObjectQuery查詢及方法

ack 支持 exec edm {0} country intersect sta 查詢

ObjectQuery 類支持對 實體數據模型 (EDM) 執行 LINQ to Entities 和 Entity SQL 查詢。ObjectQuery 還實現了一組查詢生成器方法,這些方法可用於按順序構造等效於 Entity SQL 的查詢命令。下面是 ObjectQuery 的查詢生成器方法以及等效的 Entity SQL 語句:
Distinct,Except,GroupBy,Intersect,OfType,OrderBy,Select,SelectValue,Skip,Top,Union,UnionAll,Where
每個查詢生成器方法返回 ObjectQuery 的一個新實例。使用這些方法可以構造查詢,而查詢的結果集基於前面 ObjectQuery 實例序列的操作。下面來看具體的代碼片斷:

> Execute方法:

技術分享
using (var edm = new NorthwindEntities())
      {
                string esql = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
                ObjectQuery<Customers> query = edm.CreateQuery<Customers>(esql);
                ObjectResult<Customers> results = query.Execute(MergeOption.NoTracking);
                Assert.AreEqual(results.Count(), 10);
                foreach (Customers c in query)
                    Console.WriteLine(c.CustomerID);
      }
技術分享

其中需要說明的是: MergeOption這個枚舉類型的參數項,MergeOption有四種值分別是:

> AppendOnly: 只追加新實體,不修改以前獲取的現有實體。這是默認行為。

> OverwriteChanges: 將 ObjectStateEntry 中的當前值替換為存儲區中的值。這將使用服務器上的數據重寫在本地所做的更改。

> PreserveChanges: 將替換原始值,而不修改當前值。這對於在發生開放式並發異常之後強制成功保存本地值非常有用。

> NoTracking: 將不修改 ObjectStateManager,不會獲取與其他對象相關聯的關系,可以改善性能。

> GetResultType方法:返回查詢結果的類型信息.例如:

技術分享
using (var edm = new NorthwindEntities())
            {
                string esql = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
                ObjectQuery<Customers> query = edm.CreateQuery<Customers>(esql);
                Console.WriteLine(query.GetResultType().ToString());
                //輸出結果為:
                //NorthWindModel.Customers
            }
技術分享

> ToTraceString方法:獲取當前執行的SQL語句。

> Where

實例代碼如下:

技術分享
using (var edm = new NorthwindEntities())
{
    string esql = "select value c from NorthwindEntities.Customers as c ";
    ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql);
    //使用ObjectParameter的寫法               
    query1 = query1.Where("[email protected]");
    query1.Parameters.Add(new ObjectParameter("customerid", "ALFKI"));
    //也可以這樣寫
    //ObjectQuery<Customers> query2 = edm.Customers.Where("it.CustomerID=‘ALFKI‘");
    foreach (var c in query1)
        Console.WriteLine(c.CustomerID);
    //顯示查詢執行的SQL語句
    Console.WriteLine(query1.ToTraceString());
      
    }
技術分享

> First/ FirstOrDefault

實例代碼如下:

技術分享
using (var edm = new NorthwindEntities())
{
    string esql = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
    ObjectQuery<Customers> query = edm.CreateQuery<Customers>(esql);
    Customers c1 = query.First();
    Customers c2 = query.FirstOrDefault();
    Console.WriteLine(c1.CustomerID);
    Assert.IsNotNull(c2);
    Console.WriteLine(c2.CustomerID);
}
技術分享

> Distinct

實例代碼如下:

技術分享
using (var edm = new NorthwindEntities())
{
    string esql = "select value c.City from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
    ObjectQuery<string> query = edm.CreateQuery<string>(esql);
    query = query.Distinct();
    foreach (string c in query)
    {
        Console.WriteLine("City {0}", c);
    }
}
技術分享

> Except:返回兩個查詢的差集。實例代碼如下:

技術分享
using (var edm = new NorthwindEntities())
{
    string esql1 = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
    ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql1);
    string esql2 = "select value c from NorthwindEntities.Customers as c where c.Country=‘UK‘ order by c.CustomerID limit 10";
    ObjectQuery<Customers> query2 = edm.CreateQuery<Customers>(esql2);
    query1 = query1.Except(query2);
    foreach (Customers c in query1)
    {
        Console.WriteLine(c.Country);
        //輸出:UK
    }
}
技術分享

> Intersect:返回兩個查詢的交集。實例代碼如下:

技術分享
using (var edm = new NorthwindEntities())
{
    string esql1 = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
    ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql1);
    string esql2 = "select value c from NorthwindEntities.Customers as c where c.Country=‘UK‘ order by c.CustomerID limit 10";
    ObjectQuery<Customers> query2 = edm.CreateQuery<Customers>(esql2);
    query1 = query1.Intersect(query2);
    foreach (Customers c in query1)
    {
        Console.WriteLine(c.Country);
    }
}
技術分享

> Union/UnionAll:返回兩個查詢的合集,包括重復項。其中UnionAll必須是相同類型或者是可以相互轉換的

> Include:可通過此方法查詢出與相關的實體對象。實例代碼如下:

技術分享
using (var edm = new NorthwindEntities())
{
    string esql1 = "select value c from NorthwindEntities.Customers as c WHERE c.CustomerID =‘HANAR‘";
    ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql1);
    query1 = query1.Include("Orders");
    foreach (Customers c in query1)
    {
        Console.WriteLine("{0},{1}", c.CustomerID, c.Orders.Count);
        //輸出:HANAR,14
    }

}
技術分享

> OfType: 根據制定類篩選元素創建一個新的類型。此類型是要在實體模型中已定義過的。

> OrderBy

實例代碼如下:

技術分享
using (var edm = new NorthwindEntities())
{
    string esql1 = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
    ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql1);
    query1.OrderBy("it.country asc,it.city asc");
    //也可以這樣寫               
    //query1.OrderBy("it.country asc");
    //query1.OrderBy("it.city asc");
    foreach (Customers c in query1)
    {
        Console.WriteLine("{0},{1}", c.Country, c.City);
    }
}
技術分享

> Select

實例代碼如下:

技術分享
using (var edm = new NorthwindEntities())
{
    string esql1 = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
    ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql1);
    ObjectQuery<DbDataRecord> records = query1.Select("it.customerid,it.country");
    foreach (DbDataRecord c in records)
    {
        Console.WriteLine("{0},{1}", c[0], c[1]);
    }
    Console.WriteLine(records.ToTraceString());
    //SQL輸出:
    //SELECT TOP (10)
    //1 AS [C1],
    //[Extent1].[CustomerID] AS [CustomerID],
    //[Extent1].[Country] AS [Country]
    //FROM [dbo].[Customers] AS [Extent1]
    //ORDER BY [Extent1].[CustomerID] ASC
}
技術分享


> SelectValue

實例代碼如下:

技術分享
using (var edm = new NorthwindEntities())
{
    string esql1 = "select value c from NorthwindEntities.Customers as c order by c.CustomerID limit 10";
    ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql1);
    ObjectQuery<string> records = query1.SelectValue<string>("it.customerid");
    foreach (string c in records)
    {
        Console.WriteLine("{0}", c);
    }
    Console.WriteLine(records.ToTraceString());
    //SQL輸出:
    //SELECT TOP (10)
    //[Extent1].[CustomerID] AS [CustomerID]
    //FROM [dbo].[Customers] AS [Extent1]
    //ORDER BY [Extent1].[CustomerID] ASC
}
技術分享


> Skip/Top

實例代碼如下:

技術分享
using (var edm = new NorthwindEntities())
{
    string esql1 = "select value c from NorthwindEntities.Customers as c order by c.CustomerID ";
    ObjectQuery<Customers> query1 = edm.CreateQuery<Customers>(esql1);
    query1 = query1.Skip("it.customerid asc", "10");
    query1 = query1.Top("10");
    foreach (Customers c in query1)
    {
        Console.WriteLine("{0}", c.CustomerID);
    }
    Console.WriteLine(query1.ToTraceString());
    //SQL輸出:
    //SELECT TOP (10)
    //[Extent1].[CustomerID] AS [CustomerID]
    //FROM [dbo].[Customers] AS [Extent1]
    //ORDER BY [Extent1].[CustomerID] ASC
}
技術分享

ObjectQuery查詢及方法