刚刚接触的LINQ
科普一下:
語言集成查詢(Language INtegrated Query,LINQ)
是一項(xiàng)微軟技術(shù),新增一種自然查詢的SQL語法到.NET Framework的編程語言中,可支持Visual Basic .NET以及C#語言。
?
LINQ定義了大約40個查詢操作符,如select、from、in、where以及order?by(C#中)。
使用這些操作符可以編寫查詢語句。不過,這些查詢還可以基于很多類型的數(shù)據(jù),每個數(shù)據(jù)類型都需要一個單獨(dú)的LINQ類型。
基礎(chǔ)語法:
- LINQ的關(guān)鍵詞:from, select, in, where,?group?by, orderby, …
- LINQ的注意點(diǎn):必須以select或者是group by 結(jié)束。
- LINQ的寫法:
1)from 臨時變量 in 實(shí)現(xiàn)IEnumerable<T>接口的對象
where條件表達(dá)式
[orderby條件]
[group by 條件]
select 臨時變量中被查詢的值
2) 實(shí)現(xiàn)IEnumerable接口的對象.LINQ方法名(lambda表達(dá)式)。如:
string?input?= "hellow world";
int?count?= input.Count(w=>w == 'o'); //查詢字母o出現(xiàn)的次數(shù)
能夠使用LINQ的對象需要實(shí)現(xiàn)IEnumerable<T>接口。并且LINQ的查詢表達(dá)式是在一次創(chuàng)建對象時才被編譯的。
- LINQ的全稱:Language-Integrated Query
- 命名空間:System.Linq;
注意:Linq是在.NET Framework 3.5 中出現(xiàn)的技術(shù),所以在創(chuàng)建新項(xiàng)目的時候必須要選3.5或者更高版本,否則無法使用。
選擇3.5或更高版本的.NET Framework之后,創(chuàng)建的新項(xiàng)目中會自動包含System.Linq的命名空間。
接著看代碼實(shí)現(xiàn):
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace LINQ {public class Category{public int Age { get; set; }public string Prase { get; set; }}public class Program{static void Main(string[] args){//對數(shù)據(jù)集合的操作List<string> names = new List<string> { "Tom", "Jack", "Jim", "Jackson", "Key", "Kitty" };var nameJ = from n in names where n.StartsWith("J") orderby n select n;foreach (var name in nameJ){Console.WriteLine(name);}Console.WriteLine("-------------------------");names.Add("Jack1");names.Add("Jack2");names.Add("Jack3");foreach (string name in nameJ){Console.WriteLine(name);}Console.WriteLine("-------------------------");List<Category> cs = new List<Category>() { new Category{Age=22,Prase="13期"},new Category{Age=22,Prase="12期"},new Category{Age=22,Prase="14期"},new Category{Age=23,Prase="14期"},new Category{Age=20,Prase="13期"},new Category{Age=24,Prase="14期"},new Category{Age=25,Prase="14期"},new Category{Age=23,Prase="13期"},new Category{Age=25,Prase="14期"}};var s = (from s1 in cs where s1.Age > 21 && s1.Prase == "14期" orderby s1.Age descending select s1).Take(3);foreach (var c1 in s){Console.WriteLine(c1.Age + " " + c1.Prase);}Console.WriteLine("-------------------------");var ss = from s1 in csgroup s1 by s1.Prase into corderby c.Count() descending, c.Keywhere c.Count() >= 2select c.Key + c.Count();foreach (var s1 in ss){Console.WriteLine(s1);}Console.WriteLine("-------------------------");object[] data = { "hello",33,"what",36,"fine",39,"thanks"};var str = data.OfType<string>();foreach (var a in str){Console.WriteLine(a);}Console.ReadKey();}} }
上述代碼除了LINQ查詢語法外,其他都是我們所熟悉的語法,而LINQ查詢語法跟SQL查詢語法很相似,除了先后順序。
Q:為何 LINQ 查詢語法是以 from 關(guān)鍵字開頭的,而不是以 select 關(guān)鍵字開頭的?select 開頭這種寫法跟SQL的寫法更接近,更易懂呀?
A:簡單來說,為了IDE的智能感知(Intelisence)這個功能,select 關(guān)鍵字放在后面了。
其中的 into 關(guān)鍵字表示 將前一個查詢的結(jié)果視為后續(xù)查詢的生成器,這里是跟 group by 一起使用的。
LINQ中的Group by不要跟 SQL 中的Group by 混淆,SQL由于是二維結(jié)構(gòu),Group by 的一些邏輯受二維結(jié)構(gòu)的約束,無法像 LINQ 中的Group by 這么靈活。
事實(shí)上,LINQ的查詢語法存在以下兩種形式:
查詢方法方式:(Methord Syntax)
主要利用System.Linq.Enumerable類中定義的擴(kuò)展方法和Lambda表達(dá)式方式進(jìn)行查詢
? ? ?參考文檔:Lambda 表達(dá)式(C# 編程指南):https://msdn.microsoft.com/zh-cn/library/bb397687.aspx
查詢語句方式:(Query Syntax)一種更接近SQL語法的查詢方式,可讀性更好。
使用優(yōu)點(diǎn):
1、無需復(fù)雜學(xué)習(xí)過程即可上手
2、編寫更少代碼即可創(chuàng)建完整應(yīng)用。
3、更快開發(fā)錯誤更少的應(yīng)用程序。
4、無需求助奇怪的編程技巧就可合并數(shù)據(jù)源。
5、讓新開發(fā)者開發(fā)效率更高。
6、任何對象或數(shù)據(jù)源都可以定制實(shí)現(xiàn)Linq?適配器,為數(shù)據(jù)交互帶來真正方便。
函數(shù)支持:
支持以下公共語言運(yùn)行時?(CLR) 方法和屬性,因?yàn)樗鼈兛梢栽诓樵儽磉_(dá)式中進(jìn)行轉(zhuǎn)換以包含在OData服務(wù)的請求 URI 中:
| Concat | string,concat(string,p0,string,p1) |
| Contains | bool,substringof(string,p0,string,p1) |
| EndsWith | bool,endswith(string,p0,string,p1) |
| IndexOf | int,indexof(string,p0,string,p1) |
| Length | int,length(string,p0) |
| Replace | string,replace(string,p0,string,find,string,replace) |
| Substring | string,substring(string,p0,int,pos) |
| Substring | string,substring(string,p0,int,pos,int,length) |
| ToLower | string,tolower(string,p0) |
| ToUpper | string,toupper(string,p0) |
| Trim | string,trim(string,p0) |
| Day | int,day(DateTime,p0) |
| Hour | int,hour(DateTime,p0) |
| Minute | int,minute(DateTime,p0) |
| Month | int,month(DateTime,p0) |
| Second | int,second(DateTime,p0) |
| Year | int,year(DateTime,p0) |
1也支持Visual Basic中等效的Microsoft.VisualBasic.DateAndTime的日期和時間屬性以及DatePart方法。
| Ceiling | decimal,ceiling(decimal,p0) |
| Ceiling | double,ceiling(double,p0) |
| Floor | decimal,floor(decimal,p0) |
| Floor | double,floor(double,p0) |
| Round | decimal,round(decimal,p0) |
| Round | double,round(double,p0) |
| TypeIs | bool,isof(type,p0) |
客戶端或許還可以在客戶端上計(jì)算其他 CLR 函數(shù)。對于無法在客戶端上計(jì)算以及無法轉(zhuǎn)換為有效請求URI以便在服務(wù)器上計(jì)算的任何表達(dá)式,將引發(fā)?NotSupportedException。
參考文檔:在 C# 中編寫查詢 (LINQ):https://msdn.microsoft.com/zh-cn/library/bb397900(v=VS.90).aspx?lc=2052
?
轉(zhuǎn)載于:https://www.cnblogs.com/D-E-S-I-R-E/p/4573807.html
總結(jié)