具體想看LINQ的例子可以在安裝了2008後在安裝路徑中有M$提供的好多例子。這裏主要是記錄它的一些介紹。具體研究還是看微軟提供的例子比較好點吧。
綜合查詢語言LINQ 是Visual Studio 2008 和 the .NET Framework version 3.5在數據開發方面一個重大的創新.他支持C#和VisualBasie兩種編程語言.
LINQ有以下四方面的優勢:
1. 它簡化了查詢實現的方式。如果你正準備去學習C#或Visual Basic,那首先要從學習寫LINQ查詢開始。
2. 它有統一的語法,可以查詢任何一種數據源。無論是XML 文件檔、SQL數據庫、DataSet數據集,內存在的集合,本地的或者是遠程的數據源,都可用它去實現查詢。
3. 它加強了關係形數據和實例對象的聯繫。它可以做爲一強名稱工具和設計工具去創建對象和數據間的關聯。
4. 它能在程序編譯捕捉到異常,縮短項目的開發時間。
LINQ標準操作簡單介紹
LINQ標準操作是LINQ綜合查詢語言模型中的一系列方法。大部分方法都是操作實了IEnumerable(Of T) 接口或 th interface IQueryable(Of T).接口對象的序列。這些操作可以實現查詢的聚合、排序、過濾、投影等功能。
LINQ標準操作之間的不同在於它們的執行時間和返加的數據是單個值還是一組序列。返回值爲單值的方法(聚合函數Sum,Average)它們將會在調用時立即執行;返回值爲一組系列的方法,它們將會依懶於查詢語句的執行。
下面看看看簡單的例子:對字符串數組進行分組,查詢,排序!
class Program
{
static void Main(string[] args)
{
string sentence = "This i a simle LINQ DEMO!";
string[] words = sentence.Split(' ');
var query = words.
GroupBy(w => w.Length, w => w.ToUpper()).
Select(g => new { Length = g.Key, Words = g }).
OrderBy(o => o.Length);
foreach (var obj in query)
{
Console.WriteLine("單詞的長度 {0}:", obj.Length);
foreach (string word in obj.Words)
Console.WriteLine(word);
}
Console.Read();
}
}
LINQ查詢操作必備三部曲
查詢就是一種從數據源獲取數據的表達式。這種表達式在不同語言中有不同的定義。不同的語言面向不同的數據源,比如說SQL 是面向關係型數據庫,XQuery面向XML。因此,開發員人需要爲從新數據源或數據格式中獲取數據而要學習不同的查詢表達式語法。LINQ定義了統一的語法可從不同數據源或數據格式中獲取數據,從而減少了開發員人學習的負擔,減少了工作量。在LINQ查詢中,不管什麼數據源,都可以看做爲一個對象。你可以使用同樣的代碼在XML文件,SQL數據庫,ADO。NET 數據集,.NET 集合類型,其它數據源或數據格式中查詢和傳輸數據。
所有LINQ查詢操作都必包含以下三方面內容:
1. 獲取數據源
2. 創建查詢
3. 執行查詢
下面那張圖展示了LINQ查詢操作中基本三部分內容這間的依懶關係。從圖中可看出,在執行查詢操作中必須有查詢語語的存在,否則不能從數據源中獲取數據。只有創建了查詢,執行操作纔會對數據進行迭代式處理。
下面看看怎麼用代碼實現LINQ查詢操作基本的三個方面:
static void Main(string[] args)
{
// 數據源
int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
// 創建查詢
IEnumerable<int> numQuery =
from num in numbers
where (num % 2) == 0
select num;
// 執行查詢
foreach (int j in numQuery)
{
Console.Write("{0,1} ", j);
}
Console.Read();
}
=============================================================
LINQ基本操作
首先我們創建2個類,一個Category,一個Product
public class Product
{
public string Name { get; set; }
public int CategoryID { get; set; }
}
public class Category
{
public string Name { get; set; }
public int ID { get; set; }
}
然後創建2個List的數據源
static List<Category> categories = new List<Category>()
{
new Category(){Name="Beverages", ID=001},
new Category(){Name="Condiments", ID=002},
new Category(){Name="Vegetables", ID=003},
new Category(){Name="Grains", ID=004},
new Category(){Name="Fruit", ID=005},
new Category(){Name="Other", ID=006}
};
static List<Product> products = new List<Product>()
{
new Product{Name="Cola", CategoryID=001},
new Product{Name="Tea", CategoryID=001},
new Product{Name="Mustard", CategoryID=002},
new Product{Name="Pickles", CategoryID=002},
new Product{Name="Carrots", CategoryID=003},
new Product{Name="Bok Choy", CategoryID=003},
new Product{Name="Peaches", CategoryID=005},
new Product{Name="Melons", CategoryID=007},
};
好了,現在我們開始使用LINQ了,下面使用的表達式和方法的結果是一樣的,這裏的方法指Lambda表達式
l 使用排序
1. 使用排序時使用的顯示語句
foreach (var c in orderByResult)
Console.WriteLine("ID={0},Name={1}", c.ID, c.Name);
2. 升序
表達式
var orderByResult = from c in categories
orderby c.ID //默認爲升序,也可以在後面加上ascending
select c;
方法
var orderByResult = categories.OrderBy(c=>c.ID);
3. 降序
表達式
var orderByResult = from c in categories
orderby c.ID descending
select c;
方法
var orderByResult = categories.OrderByDescending(c=>c.ID);
4. 多個屬性的升序
表達式
var orderByResult = from c in categories
orderby c.ID,c.Name
select c;
方法
var orderByResult = categories.OrderBy(c=>c.ID).ThenBy(c=>c.Name);
5. 多個屬性的降序排列
表達式
var orderByResult = from c in categories
orderby c.ID, c.Name descending
select c;
方法
var orderByResult = categories.OrderByDescending(c=>c.ID).ThenByDescending(c=>c.Name);
LINQ是什麼?
它是Language Integrated Query。
當我們要對數據庫表進行查詢的時候,我們一定會編寫"select * from sometable where ID = .."的語句。好,那我們現在根據LINQ的語法,完全可以將我們熟悉的SQL中像"select","from","where"等語句在.NET Framework環境中順利使用並且大大提高開發的效率。
下面我就牛刀小試,做個demo看看。
1. 先下載LinQ框架
現在最新版本是2006年5月發佈"Orcas CTP", 下載地址(這裏 )
2. 下載安裝待完畢。
3. 新建一個"LINQ Console Application"項目。
4. 輸入代碼如下:
2using System;
3using System.Collections.Generic;
4using System.Text;
5using System.Query;
6using System.Xml.XLinq;
7using System.Data.DLinq;
8
9namespace LINQConsoleApplication1
10{
11 class Program
12 {
13 static void Main(string[] args)
14 {
15 string[] aBunchOfWords = {"One","Two", "Hello", "World",
16
17"Four", "Five"};
18 var result =
19 from s in aBunchOfWords // query the string array
20 where s.Length == 5 // for all words with length = 5
21 select s; // and return the string
22 foreach (var s in result) {
23 Console.WriteLine(s); //print
24 }
25 }
26 }
27}
28
29
運行結果如下:
Hello
World
print any key to continue ...