.NET Framework 3.5中的LINQ簡介

來源:互聯網
上載者:User

標籤:記憶體   操作符   參數   param   數組   方式   max   binary   col   

1. LINQ 概覽

 1.1. 資料訪問現狀

 1.2. LINQ 資料訪問方式

 1.3. LINQ 項目

2. 訪問數組

 2.1. 查詢數組

 2.2. 綁定到頁面

3. 訪問集合

 3.1. 自訂 City 類

public class City{public string Name;public string Country;public int DistanceFromSeattle;}List<City> locations = GetLocations();

 3.2. 查詢City 集合

 3.3. 綁定到頁面

<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server"><Columns><asp:BoundField HeaderText="Country" DataField="Country" /><asp:BoundField HeaderText="City" DataField="Name" /><asp:BoundField HeaderText=“Dist” DataField="DistanceFromSeattle“/></Columns></asp:GridView>

 3.4. 綁定頁面結果

4. 查詢投影

 4.1. 查詢投影(Select)

  ? 不返回所有資料列/屬性
  ? 修改或者轉化查詢返回的資料
  ? 利用編譯器對“匿名型別”的支援查詢資料列/屬性
  ? 產生匿名型別(’a)

 4.2. 使用匿名型別

 4.3. 匿名型別綁定

<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server"><Columns><asp:BoundField HeaderText="Country" DataField="Country" /><asp:BoundField HeaderText="City" DataField=“City" /><asp:BoundField HeaderText="Dist (KM)" DataField="DistanceInKm" /></Columns></asp:GridView>

 4.4. 匿名型別綁定結果

5. 使用 λ 運算式

 5.1. 使用λ 運算式

 ? 查詢文法是一個方便的聲明性代碼縮寫您,可以手動編寫它:
 ? IEnumerable expr = names
  .Where(s => s.Length == 5)
  .OrderBy(s => s)
  .Select(s => s.ToUpper());

 5.2. λ 運算式的委託聲明
 ? λ 運算式是C# 2 0 匿名方法的自然演化結果

Func filter = delegate (string s) {return s.Length == 5;};Func extract = delegate (string s) {return s;};Func project = delegate (string s) {return s.ToUpper();};IEnumerable expr = names.Where(filter).OrderBy(extract).Select(project);

 5.3. 運算式樹狀架構
 . 運算式樹狀架構是λ 運算式的有效記憶體中資料表示形式,它使運算式的結構透明且顯式。
 . 將λ 運算式指定給Expression 類型的變數、欄位或參數,則編譯器將發出運算式樹狀架構。

BinaryExpression body = (BinaryExpression)filter.Body;ParameterExpression left = (ParameterExpression)body.Left;ConstantExpression right = (ConstantExpression)body.Right;Console.WriteLine("{0} {1} {2}", left.Name, body.NodeType,right.Value);

6. 查詢操作符

 6.1. 查詢操作符Where 的定義

public static class Sequence {  public static IEnumerable Where(this IEnumerable source,Func predicate) {foreach (T item in source)if (predicate(item))yield return item;}}

 6.2. 調用
 ? 普通的方式來調用擴充方法:
 ? IEnumerable<string> query =Enumerable.Where(names,s => s.Length < 6);
 ? C#語言允許我們使用如下的方式來調用擴充方法:
 ? IEnumerable<string> query = names.Where(s =>s.Length < 6);

 6.3. 標準查詢操作符
 . 排序與分組
  – OrderBy & GroupBy
 . 彙總
  – Count
  – Sum
  – Average
  – Max
  – Min
 . 投影
  – Select & SelectMany

 6.3. 查詢文法
 ? C# 的現有 foreach 語句通過 .NET Framework 的IEnumerable/IEnumerator 方法為迭代提供聲明
性文法。foreach 語句完全是可選的,但經過證實,它是一個非常方便和常用的語言機制。
 ? 查詢文法通過聲明性文法為以下最常用的查詢操作符簡化了查詢運算式:Where、Select、
SelectMany、GroupBy、OrderBy、ThenBy、OrderByDescending 和ThenByDescending。

 

.NET Framework 3.5中的LINQ簡介

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.