zoukankan      html  css  js  c++  java
  • 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 stringList
                where 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 studentList
                          where s.Age > 12 && s.Age < 20
                          select 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:

     

    标准查询操作根据功能分类 

    ClassificationStandard Query Operators
    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
  • 相关阅读:
    大型运输行业实战_day03_1_基于intellij idea的非maven spring+springMVC+mybatis搭建
    大型运输行业实战_day02_2_数据模型建立
    大型运输行业实战_day02_1_数据库设计与powerDesigner使用
    MySQL 并发控制(锁得使用)
    Oracle 日期减年数、两日期相减
    Oracle 递归拼接字段
    设计模式之适配器模式(结构型)
    设计模式之桥接模式(结构型)
    设计模式之装饰模式(结构型)
    Redis学习笔记之位图
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6602024.html
Copyright © 2011-2022 走看看