linq---我為你提筆序,你的美不只查詢語句,linq---提筆

來源:互聯網
上載者:User

linq---我為你提筆序,你的美不只查詢語句,linq---提筆

        LinQ百度百科對她這樣解釋,是一組用於c#和Visual Basic語言的擴充。它允許編寫C#或者Visual Basic代碼以查詢資料庫相同的方式操作記憶體資料。 LINQ是Language Integrated Query的簡稱,翻譯成中文就是Language-integrated Query (LINQ),它是整合在.NET程式設計語言中的一種特性。已成為程式設計語言的一個組成部分,在編寫程式時可以得到很好的編譯時間語法檢查,豐富的中繼資料,智能感知、靜態類型等強型別語言的好處。並且它同時還使得查詢可以方便地對記憶體中的資訊進行查詢而不僅僅只是外部資料源。她提供多種查詢方法,有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更加簡潔,接下來,小編簡單的介紹一下linq的擴充方法,那麼什麼是擴充方法呢?擴充方法又是如何出現的呢? .net framework為編程人員提供了很多的類,很多的方法,但是,不論.net framework在類中為我們提供了多麼多的方法,有時候仍然不能滿足我們的需求,例如:你想讓字串對象具有ToPascal方法,含義就是將字串轉化為Pascal格式,並返回,我們知道,.net framework提供的String類中並沒有為我們提供相應的方法,此時,我們應該怎麼做才可以達到我們的目的呢?有人說可以繼承String類,這個不行,因為.net framework提供的類都是finnal類,最終類,不能被繼承,那麼,怎麼樣才可以解決這個問題呢?此時,就出現了擴充方法。擴充方法有哪些好處呢? 為現有類提供一些額外的方法,這樣做的好處就是,原有類不需要重新編譯產生,只需要在程式中引入一些額外的dll就可以了,我們來看一個具體的例子。

        比如說,我們有個字串,當我們定義一個變數之後,比如說,我們給他一個值,在s字串裡面有一組方法,那麼這組方法是系統幫我們定義好的,如果有天,我想要一個特殊的方法,比如topuuer轉換成大寫,tolower轉換成小寫,在接著,我想要首字母大寫後面都小寫,怎麼辦,這裡面沒有這種方法,怎麼辦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 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>
       運行效果如下:

       

       最後,小編來簡單的介紹一下lambda運算式,C#Lambda基本的表達形式:(參數列表) => {方法體},說明一下,參數列表中的參數類型可以是明確類型或者是推斷類型,如果是推斷類型,則參數的資料類型將由編譯器根據上下文自動推斷出來,Lambda 用在基於方法的 LINQ 查詢中,作為諸如Where 和 Where 等標準查詢運算子方法的參數。我們來看一個具體的例子:

<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      {          //定義一個委託          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的一些基本知識,包括linq和sql的對比,linq的擴充方法以及lambda運算式,對linq的理解還不是很深刻,歡迎小夥伴們一起討論交流,Linq的一個重要功能就是ORMapping思想的具體實現,ORMapping的思想就是實現編程的真正物件導向,使我們不用關心資料庫,只需要關係實體類或實體集合類就行。項目進行中,未完待續......

相關文章

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.