LINQ TO SQL八大子句

LINQ TO SQL八大子句

一、form...in子句

        含義:指定查詢操作的數據源和範圍變量

        在 LINQ 查詢中,第一步是指定數據源。像在大多數編程語言中一樣,在 C# 中,必須先聲明變量,才能使用它。在 LINQ 查詢中,最先使用 from 子句的目的是引入數據源 ( customers) 和範圍變量 ( cust)。 

//queryAllCustomers is an IEnumerable<Customer>
  
  var queryAllCustomers = from cust in customers
                          select cust;

二、select子句

        含義:指定查詢結果的類型和表現形式

        select 子句生成查詢結果並指定每個返回的元素的“形狀”或類型。 例如,您可以指定結果包含的是整個 Customer 對象、僅一個成員、成員的子集,還是某個基於計算或新對象創建的完全不同的結果類型。 當 select 子句生成除源元素副本以外的內容時,該操作稱爲“投影”。 使用投影轉換數據是 LINQ 查詢表達式的一種強大功能。

三、where子句

        含義:篩選元素的邏輯條件,一般由邏輯運算符組成

        也許最常用的查詢操作是應用布爾表達式形式的篩選器。此篩選器使查詢只返回那些表達式結果爲 true 的元素。使用 where 子句生成結果。 實際上,篩選器指定從源序列中排除哪些元素。在下面的示例中,只返回那些地址位於倫敦的 customers

var queryLondonCustomers = from cust in customers
                             where cust.City == "London"
                             select cust;

四、group...by子句

        含義:對查詢進行分組

        使用 group 子句,您可以按指定的鍵分組結果。 例如,您可以指定結果應按 City 分組,以便位於倫敦或巴黎的所有客戶位於各自組中。 在本例中, cust.City 是鍵。

// queryCustomersByCity is an IEnumerable<IGrouping<string, Customer>>
  
    var queryCustomersByCity =
        from cust in customers
        group cust by cust.City;
  
    // customerGroup is an IGrouping<string, Customer>
  
    foreach (var customerGroup in queryCustomersByCity)
    {
        Console.WriteLine(customerGroup.Key);
        foreach (Customer customer in customerGroup)
        {
            Console.WriteLine("    {0}", customer.Name);
        }
    }
        在使用 group 子句結束查詢時,結果採用列表的列表形式。 列表中的每個元素是一個具有 Key 成員及根據該鍵分組的元素列表的對象。 在循環訪問生成組序列的查詢時,您必須使用嵌套的 foreach 循環。 外部循環用於循環訪問每個組,內部循環用於循環訪問每個組的成員。

五、order by子句

        含義:對查詢結果進行排序,可以爲“升序”或“降序”

        通常可以很方便地將返回的數據進行排序。orderby 子句將使返回的序列中的元素按照被排序的類型的默認比較器進行排序。 例如,下面的查詢可以擴展爲按 Name 屬性對結果進行排序。 因爲 Name 是一個字符串,所以默認比較器執行從 A 到 Z 的字母排序。

var queryLondonCustomers3 = 
      from cust in customers
      where cust.City == "London"
      orderby cust.Name ascending
      select cust;

六、join子句

        含義:連接多個查詢的操作源

        聯接運算創建數據源中沒有顯式建模的序列之間的關聯。例如,您可以執行聯接來查找符合以下條件的所有客戶:位於巴黎,且從位於倫敦的供應商處訂購產品。在 LINQ 中, join 子句始終針對對象集合而非直接針對數據庫表運行。 在 LINQ 中,您不必像在 SQL 中那樣頻繁使用 join,因爲 LINQ 中的外鍵在對象模型中表示爲包含項集合的屬性。 例如, Customer 對象包含 Order 對象的集合。 不必執行聯接,只需使用點表示法訪問訂單:


from order in Customer.Orders...
  

七、let子句

        含義:引入用於存儲查詢表達式中的子表達式結果的範圍變量

        在查詢表達式中,存儲子表達式的結果有時很有用,這樣可以在隨後的子句中使用。可以使用 let 關鍵字完成這一工作,該關鍵字可以創建一個新的範圍變量,並且用您提供的表達式的結果初始化該變量。 一旦用值初始化了該範圍變量,它就不能用於存儲其他值。但如果該範圍變量存儲的是可查詢的類型,則可以對其進行查詢。

  1. 創建一個可以查詢自身的可枚舉類型。

  2. 使查詢只能對範圍變量 word 調用一次 ToLower。 如果不使用 let,則必須在 where 子句的每個謂詞中調用 ToLower

class LetSample1
  {
      static void Main()
      {
          string[] strings = 
          {
              "A penny saved is a penny earned.",
              "The early bird catches the worm.",
              "The pen is mightier than the sword." 
          };
  
          // Split the sentence into an array of words
  
          // and select those whose first letter is a vowel.
  
          var earlyBirdQuery =
              from sentence in strings
              let words = sentence.Split(' ')
              from word in words
              let w = word.ToLower()
              where w[0] == 'a' || w[0] == 'e'
                  || w[0] == 'i' || w[0] == 'o'
                  || w[0] == 'u'
              select word;
  
          // Execute the query.
  
          foreach (var v in earlyBirdQuery)
          {
              Console.WriteLine("\"{0}\" starts with a vowel", v);
          }
  
          // Keep the console window open in debug mode.
  
          Console.WriteLine("Press any key to exit.");
          Console.ReadKey();
      }
  }
  /* Output:
    "A" starts with a vowel
    "is" starts with a vowel
    "a" starts with a vowel
    "earned." starts with a vowel
    "early" starts with a vowel
    "is" starts with a vowel
*/

八、into子句

        含義:提供一個臨時標示符,充當對join、group或select子句的結果

        如果您必須引用組操作的結果,可以使用 into 關鍵字來創建可進一步查詢的標識符。 下面的查詢只返回那些包含兩個以上的客戶的組:

// custQuery is an IEnumerable<IGrouping<string, Customer>>
  
  var custQuery =
      from cust in customers
      group cust by cust.City into custGroup
      where custGroup.Count() > 2
      orderby custGroup.Key
      select custGroup;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章