• C# LINQ语法


    from子句嵌套

    private void button5_Click(object sender, EventArgs e)
            {
                listBox1.Items.Clear();
    
                List<Student1> students = new List<Student1>
                {
                    new Student1{Name = "张三",Scores = new List<int>{93,74,94,58}},
                    new Student1{Name = "李四",Scores = new List<int>{94,74,86,58}},
                    new Student1{Name = "王五",Scores = new List<int>{95,74,86,58}},
                    new Student1{Name = "赵六",Scores = new List<int>{96,74,86,58}},
                    new Student1{Name = "冯七",Scores = new List<int>{97,74,86,58}}
                };
    
                var scoreQuery =
                    from student in students
                    from scores in student.Scores
                    where scores > 90
                    select new { name = student.Name, scores };
                foreach (var stu in scoreQuery)
                {
                    listBox1.Items.Add(string.Format("{0}超过90分的成绩有{1}",stu.name,stu.scores));
                }
            }

    Form子句交叉连接查询

    private void button6_Click(object sender, EventArgs e)
            {
                char[] upperCase = { 'A', 'B', 'C' };
                char[] lowerCase = { 'x', 'y', 'z' };
    
                var joinQuery1 =
                    from upper in upperCase
                    from lower in lowerCase
                    select new { upper, lower };
    
                var joinQuery2 =
                    from lower in lowerCase
                    where lower != 'x'
                    from upper in upperCase
                    select new { upper, lower };
    
                foreach (var pair in joinQuery1)
                {
                    listBox1.Items.Add(string.Format("{0}匹配{1}",pair.upper,pair.lower));
                }
    
                listBox1.Items.Add("----我是无聊的分界线----");
    
                foreach (var pair in joinQuery2)
                {
                    listBox1.Items.Add(string.Format("{0}匹配{1}", pair.lower, pair.upper));
                }
            }

    select语句投影

    使用select子句将数据源转换为新类型的序列

    group分组

    group *** by ****;

    into 关键字

    group关键字的同时配合使用into关键字。

    let子句

    let子句可以将表达式(如方法调用)的结果存储到新的范围变量中

    var xxx =
                    from x in xxx
                    let sum_x = x.Scores[0] + x.Scores[1] + x.Scores[2]
                    where sum_x/3 > 80
                    select string.Format("平均值大于80的有{0},平均分为{1}",x.name,sum_x/4);
  • 相关阅读:
    [OpenWRT]判断WDS是否开启
    【cocos2d-js官方文档】一、搭建 Cocos2d-JS 开发环境
    noi Big String 超级字符串
    序列 xulie (2017青岛)
    %%%城市交通费 city //程序超时
    3.密码pasuwado————记第一次超越Candy?
    图论-欧拉回路(邻接链表)
    blue and red ball
    魔方→︿←
    The first DP!
  • 原文地址:https://www.cnblogs.com/Mysterious/p/3427667.html
Copyright © 2020-2023  润新知