LINQ 学习路程 -- 查询语法 LINQ Query Syntax
生活随笔
收集整理的這篇文章主要介紹了
LINQ 学习路程 -- 查询语法 LINQ Query Syntax
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.查詢語(yǔ)法?
Query Syntax:
from <range variable> in <IEnumerable<T> or IQueryable<T> Collection><Standard Query Operators> <lambda expression><select or groupBy operator> <result formation>?
// string collection IList<string> stringList = new List<string>() { "C# Tutorials","VB.NET Tutorials","Learn C++","MVC Tutorials" ,"Java" };// LINQ Query Syntax var result = from s in stringListwhere s.Contains("Tutorials") select s;
查詢語(yǔ)法以From開(kāi)頭,后面緊跟著Range veriable變量,From從句像這樣的結(jié)構(gòu)"From?rangeVariableName?in?IEnumerablecollection",意思是從集合的每個(gè)對(duì)象中獲取,它有點(diǎn)像foreach循環(huán),
?foreach(Student s in studentList)
在From從句之后,我們可以使用不同的標(biāo)準(zhǔn)查詢運(yùn)算符來(lái)過(guò)濾、分類和聯(lián)合集合里面的元素,大概有50個(gè)標(biāo)準(zhǔn)查詢操作
一般以Select 或Group從句結(jié)尾,Select從句用來(lái)構(gòu)建數(shù)據(jù),你可以查詢?nèi)繉?duì)象或者它的幾個(gè)屬性。
?
IList<Student> studentList = new List<Student>>() { new Student() { StudentID = 1, StudentName = "John", Age = 13} ,new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 } };// LINQ Query Syntax to find out teenager students var teenAgerStudent = from s in studentListwhere s.Age > 12 && s.Age < 20select s;?
?
LINQ Method Syntax
// string collection IList<string> stringList = new List<string>() { "C# Tutorials","VB.NET Tutorials","Learn C++","MVC Tutorials" ,"Java" };// LINQ Query Syntax var result = stringList.Where(s => s.Contains("Tutorials"));?
?
?
// Student collection IList<Student> studentList = new List<Student>() { new Student() { StudentID = 1, StudentName = "John", Age = 13} ,new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 } };// LINQ Method Syntax to find out teenager students var teenAgerStudents = studentList.Where(s => s.Age > 12 && s.Age < 20).ToList<Student>();?
?
?
Standard Query Operators:
Standard Query Operators in Query Syntax:
?
Standard Query Operators in Method Syntax:
?
標(biāo)準(zhǔn)查詢操作根據(jù)功能分類?
| Filtering(篩選) | Where, OfType |
| Sorting(排序) | OrderBy, OrderByDescending, ThenBy, ThenByDescending, Reverse |
| Grouping(分組) | GroupBy, ToLookup |
| Join(連接) | GroupJoin, Join |
| Projection(投影) | Select, SelectMany |
| Aggregation(聚集) | Aggregate, Average, Count, LongCount, Max, Min, Sum |
| Quantifiers(量詞) | All, Any, Contains |
| Elements(元素) | ElementAt, ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault |
| Set(集合) | Distinct, Except, Intersect, Union |
| Partitioning(分割) | Skip, SkipWhile, Take, TakeWhile |
| Concatenation(連接) | Concat |
| Equality | SequenceEqual |
| Generation | DefaultEmpty, Empty, Range, Repeat |
| Conversion | AsEnumerable, AsQueryable, Cast, ToArray, ToDictionary, ToList |
?
轉(zhuǎn)載于:https://www.cnblogs.com/lanpingwang/p/6602024.html
總結(jié)
以上是生活随笔為你收集整理的LINQ 学习路程 -- 查询语法 LINQ Query Syntax的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: VM虚拟机ping不通局域网其他主机的解
- 下一篇: nginx正确服务react-route