linq---我為你提筆序,你的美不只查詢語句
來源:程序員人生 發布時間:2015-01-26 09:27:01 閱讀次數:3447次
LinQ百度百科對她這樣解釋,是1組用于c#和Visual Basic語言的擴大。它允許編寫C#或Visual Basic代碼以查詢數據庫相同的方式操作內存數據。 LINQ是Language Integrated Query的簡稱,翻譯成中文就是語言集成查詢,它是集成在.NET編程語言中的1種特性。已成為編程語言的1個組成部份,在編寫程序時可以得到很好的編譯時語法檢查,豐富的元數據,智能感知、靜態類型等強類型語言的好處。并且它同時還使得查詢可以方便地對內存中的信息進行查詢而不單單只是外部數據源。她提供多種查詢方法,有select,where,orderby,groupby,小火伴們是否是覺得在哪兒見過nie,哦,在夢里,開玩笑,跟我們之前學習過的sql相似,那末她們有甚么區分nie,接著小編舉例說明。
比如,我們要實現從numbers數組中獲得大于50的數,我們的代碼是這么來寫的:
<span style="font-size:18px;"><span style="font-size:18px;">using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ch01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnTest_Click(object sender, EventArgs e)
{
int[] arr = { 123, 12, 3, 12, 15, 4, 6, 46, 48, 56, 785 };
//獲得大于50的數
//沒有LinQ我們怎樣做?
ArrayList result = new ArrayList();
for (int i = 0; i < arr.Length;i++ )
{
if (arr[i]> 50)
{
result.Add(arr[i]);
}
}
//打印result就ok了
for (int i = 0;i<result.Count;i++)
{
Console.WriteLine(result[i]);
}
}
}
}
</span></span>
運行效果以下:

接著,我們來看,如果使用linq,我們的代碼又該如何寫nie:
<span style="font-size:18px;">using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ch01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnTest_Click(object sender, EventArgs e)
{
int[] arr = { 123, 12, 3, 12, 15, 4, 6, 46, 48, 56, 785 };
//有了LinQ后我們可以這么做
//獲得大于50的數
IEnumerable ie = arr.Select(p => p).Where(p => p > 50);
//輸出--該部份代碼可重用
IEnumerator result = ie.GetEnumerator();
while (result.MoveNext())
{
Console.WriteLine(result.Current);
}
}
}
}
</span>
運行效果以下:

從上面的對照我們很容易可以看出,使用linq更加簡潔,接下來,小編簡單的介紹1下linq的擴大方法,那末甚么是擴大方法呢?擴大方法又是如何出現的呢? .net framework為編程人員提供了很多的類,很多的方法,但是,不論.net framework在類中為我們提供了多么多的方法,有時候依然不能滿足我們的需求,例如:你想讓字符串對象具有ToPascal方法,含義就是將字符串轉化為Pascal格式,并返回,我們知道,.net framework提供的String類中并沒有為我們提供相應的方法,此時,我們應當怎樣做才可以到達我們的目的呢?有人說可以繼承String類,這個不行,由于.net
framework提供的類都是finnal類,終究類,不能被繼承,那末,怎樣樣才可以解決這個問題呢?此時,就出現了擴大方法。擴大方法有哪些好處呢? 為現有類提供1些額外的方法,這樣做的好處就是,原有類不需要重新編譯生成,只需要在程序中引入1些額外的dll就能夠了,我們來看1個具體的例子。
比如說,我們有個字符串,當我們定義1個變量以后,比如說,我們給他1個值,在s字符串里面有1組方法,那末這組方法是系統幫我們定義好的,如果有天,我想要1個特殊的方法,比如topuuer轉換成大寫,tolower轉換成小寫,在接著,我想要首字母大寫后面都小寫,怎樣辦,這里面沒有這類方法,怎樣辦nie,怎樣去實現這類功能,這個就是我們擴大方法中要解決的問題,擴大方法的目標就是對現有的類提供額外的方法以增強該類的功能,我們可以這么來做,給他加1個擴大方法,首先,我們來做1個靜態類,擴大方法是1種特殊的靜態方法,我們怎樣樣來實現呢,以下:
<span style="font-size:18px;">using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ch01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
private void btnExtraMethod_Click(object sender, EventArgs e)
{
//拓展方法,目標,對現有的類提供額外的方法以增強類的功能
string s = "asEdSadaDsdSsa";
Console.WriteLine(s.ToUpper());
Console.WriteLine(s.ToLower());
Console.WriteLine(s.ToPascal());
Console.WriteLine(s.ToPascal(2));
}
//將字符串的手寫字母轉換為大寫字母的方法
public string toPascal(string s)
{
return s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower();
}</span>
運行效果以下:

最后,小編來簡單的介紹1下lambda表達式,C#Lambda基本的表達情勢:(參數列表) => {方法體},說明1下,參數列表中的參數類型可以是明確類型或是推斷類型,如果是推斷類型,則參數的數據類型將由編譯器根據上下文自動推斷出來,Lambda 用在基于方法的 LINQ 查詢中,作為諸如Where 和 Where 等標準查詢運算符方法的參數。我們來看1個具體的例子:
<span style="font-size:18px;">using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
public partial class Form1 : Form
{
//定義1個拜托
public delegate string deleTransfer(string s);
public Form1()
{
InitializeComponent();
}
private void btnTest_Click(object sender, EventArgs e)
{
//拓展方法----
string strTest = "asdsad";
Console.WriteLine(strTest.ToLower());
Console.WriteLine(strTest.ToUpper());
Console.WriteLine(strTest.ToPascal());
Console.WriteLine("-------------------------------------");
//Lambda 來源
//.Net FrameWork 1.0拜托---函數指針
deleTransfer trans = new deleTransfer(ToPascal); //拜托指向方法ToPascal
Console.WriteLine(trans("abcdEFGH"));
//.net 2.0 匿名方法
deleTransfer transs = delegate(string s) { return s.Substring(0,1).ToUpper() + s.Substring(1).ToLower(); };
Console.WriteLine(transs("abcdEFGH"));
//.net 3.5 匿名方法
//deleTransfertransss = (s) => (s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower());
deleTransfer transss = s =>s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower();
Console.WriteLine(transss("abcdEFGH"));
}
//將字符串的首字母轉化為大寫字母的方法
public string ToPascal(string s)
{
return s.Substring(0,1).ToUpper() + s.Substring(1).ToLower();
}
}
public static class ExtraClass
{
//拓展方法,特殊的靜態方法
public static string ToPascal(this string s) //this后帶類型,表名為該類型添加特殊的方法
{
return s.Substring(0,1).ToUpper() + s.Substring(1).ToLower();
}
public static string ToPascal(this string s, int len) //this后帶類型,表名為該類型添加特殊的方法
{
return s.Substring(0,1).ToUpper() + s.Substring(1, len).ToLower() + s.Substring(len + 1);
}
}
</span>
它的發展進程是在C#1.0中使用的拜托,C#2.0中添加了匿名方法,C#3.0中添加了lambda表達式。
小編寄語:該博文,小編主要簡單的介紹了linq的1些基本知識,包括linq和sql的對照,linq的擴大方法和lambda表達式,對linq的理解還不是很深入,歡迎小火伴們1起討論交換,Linq的1個重要功能就是ORMapping思想的具體實現,ORMapping的思想就是實現編程的真正面向對象,使我們不用關心數據庫,只需要關系實體類或實體集合類就行。項目進行中,未完待續......
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈