LINQ入門簡單描述
語言集成查詢 (LINQ)有很多值得學(xué)習(xí)的地方,這里我們主要介紹一下LINQ入門,包括介紹LINQ查詢語言等方面。
語言集成查詢 (LINQ) 是 Visual Studio 2008 和 .NET Framework 3.5 版中一項突破性的創(chuàng)新,它在對象領(lǐng)域和數(shù)據(jù)領(lǐng)域之間架起了一座橋梁。
傳統(tǒng)上,針對數(shù)據(jù)的查詢都是以簡單的字符串表示,而沒有編譯時類型檢查或 IntelliSense 支持。此外,您還必須針對以下各種數(shù)據(jù)源學(xué)習(xí)不同的查詢語言:SQL 數(shù)據(jù)庫、XML 文檔、各種 Web 服務(wù)等。LINQ 使查詢成為 C# 和 Visual Basic 中的一等語言構(gòu)造。應(yīng)用于所有信息源( all sources of information )的具有多種用途( general-purpose )的語法查詢特性( query facilities )。對所有信息源的查詢語句,類似數(shù)據(jù)庫的sql語句,xml的xpath 。
LINQ入門代碼:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace HelloVS2008
- {
- public partial class LinqFrm : Form
- {
- public LinqFrm()
- {
- InitializeComponent();
- }
- private void btnLinq_Click(object sender, EventArgs e)
- {
- int[] arr = new int[] { 8, 5, 89, 3, 56, 4, 1, 58 };//信息源
- //var申請的是無類型變量。n沒有定義就使用了,類似javascript語法
- //正常的次序:select item from item in items where item>5 orderby item 就是把select item置后
- //與sql的區(qū)別:select col1 from table 這里似乎是select col1 from col1 in table 不知何故
- var m = from n in arr where n < 5 orderby n select n;
- foreach (var n in m)
- {
- txtLinq.Text += n;
- }
- }
- }
- }
以上是LINQ入門介紹
【編輯推薦】