利用LINQ做簡單的字串陣列查詢
最近因為VS2008正式版出來了...小弟也開始在玩LINQ了....
小弟做了一個超級簡單的範例...介紹如何使用LINQ這個新技術
環境:Visual Studio 2008 + .NET Framework 3.5
asp.net(c#)範例
01 using System;
02 using System.Collections;
03 using System.Configuration;
04 using System.Data;
05 using System.Linq;
06 using System.Web;
07 using System.Web.Security;
08 using System.Web.UI;
09 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13 using System.Collections.Generic;
14
15 public partial class LinqTest : System.Web.UI.Page
16 {
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 //文字資料庫
20 string[] StrDB = { "Puma", "BlueShop", "Microsoft", "Linq" };
21
22 //找出長度大於5的字串,並且排序
23 IEnumerable<string> StrSelete = from str in StrDB where str.Length > 5 orderby str select str;
24
25 //列出查詢結果
26 foreach (string item in StrSelete)
27 {
28 Response.Write(item + "<br/>");
29 }
30 }
31 }
32
33
34
02 using System.Collections;
03 using System.Configuration;
04 using System.Data;
05 using System.Linq;
06 using System.Web;
07 using System.Web.Security;
08 using System.Web.UI;
09 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13 using System.Collections.Generic;
14
15 public partial class LinqTest : System.Web.UI.Page
16 {
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 //文字資料庫
20 string[] StrDB = { "Puma", "BlueShop", "Microsoft", "Linq" };
21
22 //找出長度大於5的字串,並且排序
23 IEnumerable<string> StrSelete = from str in StrDB where str.Length > 5 orderby str select str;
24
25 //列出查詢結果
26 foreach (string item in StrSelete)
27 {
28 Response.Write(item + "<br/>");
29 }
30 }
31 }
32
33
34
執行結果:
BlueShop
Microsoft