zoukankan      html  css  js  c++  java
  • LINQ之查询语法select子句

    select 子句主要用于返回在执行查询时所需要的数据,该子句主要基于前面子句的计算结果以及select本身的所有表达式。

    select子句是查询表达式的一种结束方式,另一种是用group结束。

    select也可以直接返回范围变量,这是最简单的情况。

    select子句也非常简单,有一点需要注意,如果我们返回的是一个匿名对象,并且在select子句上做了操作,那么,我们应该给操作后的数据赋予一个名字,看下面的例子:

                //定义数据源
                List<Student> students = new List<Student>
                                               {
                                                   new Student{Name = "Terry",Scores = new List<int>{98,88,93,75,82}},
                                                   new Student{Name = "Tina",Scores = new List<int>{85,99,87,93,97}},
                                                   new Student{Name = "Linda",Scores = new List<int>{57,100,83,89,92}},
                                                   new Student{Name = "Leon",Scores = new List<int>{100,98,72,77,84}},
                                                   new Student{Name = "Echo",Scores = new List<int>{79,80,97,55,88}}
                                               };
                //定义查询表达式,找出不及格的学生及其分数
                var scoreQuery = from student in students
                                 from score in student.Scores
                                 where score < 60
                                 select new {student.Name, ActualScore = score*1.1 };
    
                //输出结果
                foreach (var query in scoreQuery)
                {
                    Console.WriteLine("Student:{0} Score:{1}",query.Name,query.ActualScore);
                }

    在上面的例子中,我们返回score的时候,做了*1.1的处理,如果我们没有增加一个显式名称,VS就会报错。因为当我们执行查询的时候,就不知道如何去取查询表达式中计算之后的数据。

  • 相关阅读:
    解决uniapp中app.vue的onlaunch不能跳转页面问题
    参数校验注解,备用
    码云推送项目总是没有权限
    一句话解释回调函数
    动态管理
    转:用jupyter notebook打开指定目录下的.ipynb文件
    gcn变体
    图4
    节点分类
    图3
  • 原文地址:https://www.cnblogs.com/tian2010/p/2394968.html
Copyright © 2011-2022 走看看