LINQ 学习路程 -- 查询语法 LINQ Query Syntax
生活随笔
收集整理的這篇文章主要介紹了
LINQ 学习路程 -- 查询语法 LINQ Query Syntax
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.查詢語法?
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;
查詢語法以From開頭,后面緊跟著Range veriable變量,From從句像這樣的結構"From?rangeVariableName?in?IEnumerablecollection",意思是從集合的每個對象中獲取,它有點像foreach循環,
?foreach(Student s in studentList)
在From從句之后,我們可以使用不同的標準查詢運算符來過濾、分類和聯合集合里面的元素,大概有50個標準查詢操作
一般以Select 或Group從句結尾,Select從句用來構建數據,你可以查詢全部對象或者它的幾個屬性。
?
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:
?
標準查詢操作根據功能分類?
| 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 |
?
轉載于:https://www.cnblogs.com/lanpingwang/p/6602024.html
總結
以上是生活随笔為你收集整理的LINQ 学习路程 -- 查询语法 LINQ Query Syntax的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VM虚拟机ping不通局域网其他主机的解
- 下一篇: 路由器的安装方法步骤 路由器的如何安装