• LINQ 学习路程 -- 查询例子


    IList<Student> studentList = new List<Student>() { 
        new Student() { StudentID = 1, StudentName = "John", Age = 18, StandardID = 1 } ,
        new Student() { StudentID = 2, StudentName = "Steve",  Age = 21, StandardID = 1 } ,
        new Student() { StudentID = 3, StudentName = "Bill",  Age = 18, StandardID = 2 } ,
        new Student() { StudentID = 4, StudentName = "Ram" , Age = 20, StandardID = 2 } ,
        new Student() { StudentID = 5, StudentName = "Ron" , Age = 21 } 
    };
    
    IList<Standard> standardList = new List<Standard>() { 
        new Standard(){ StandardID = 1, StandardName="Standard 1"},
        new Standard(){ StandardID = 2, StandardName="Standard 2"},
        new Standard(){ StandardID = 3, StandardName="Standard 3"}
    };

    1.多个select和where操作

    var studentNames = studentList.Where(s => s.Age > 18)
                                  .Select(s => s)
                                  .Where(st => st.StandardID > 0)
                                  .Select(s => s.StudentName);
    var teenStudentsName = from s in studentList
                           where s.age > 12 && s.age < 20
                           select new { StudentName = s.StudentName };
    
    teenStudentsName.ToList().ForEach(s => Console.WriteLine(s.StudentName));

    2.Group by

    var studentsGroupByStandard = from s in studentList
                                  group s by s.StandardID into sg
                                  orderby sg.Key 
                                        select new { sg.Key, sg };
    
    
    foreach (var group in studentsGroupByStandard)
    {
        Console.WriteLine("StandardID {0}:", group.Key);
        
        group.sg.ToList().ForEach(st => Console.WriteLine(st.StudentName ));
    }

    left outer join

    var studentsGroup = from stad in standardList
                        join s in studentList
                        on stad.StandardID equals s.StandardID
                            into sg
                            select new { 
                                            StandardName = stad.StandardName, 
                                            Students = sg 
                                        };
    
    foreach (var group in studentsGroup)
    {
        Console.WriteLine(group.StandardName);
        
        group.Students.ToList().ForEach(st => Console.WriteLine(st.StudentName));
    }
    var studentsWithStandard = from stad in standardList
                               join s in studentList
                               on stad.StandardID equals s.StandardID
                               into sg
                                   from std_grp in sg 
                                   orderby stad.StandardName, std_grp.StudentName 
                                   select new { 
                                                    StudentName = std_grp.StudentName, 
                                                    StandardName = stad.StandardName 
                                    };
    
    
    foreach (var group in studentsWithStandard)
    {
        Console.WriteLine("{0} is in {1}", group.StudentName, group.StandardName);
    }

    Sorting

    var sortedStudents = from s in studentList
                            orderby s.StandardID, s.age
                            select new { 
                                    StudentName = s.StudentName, 
                                    Age = s.age, 
                                    StandardID = s.StandardID };
    
    sortedStudents.ToList().ForEach(s => Console.WriteLine("Student Name: {0}, Age: {1}, StandardID: {2}", s.StudentName, s.Age , s.StandardID));

    inner join

    var studentWithStandard = from s in studentList
                              join stad in standardList
                              on s.StandardID equals stad.StandardID 
                              select new { 
                                      StudentName = s.StudentName, 
                                      StandardName = stad.StandardName 
                                  };
    var nestedQueries = from s in studentList
                        where s.age > 18 && s.StandardID == 
                            (from std in standardList
                            where std.StandardName == "Standard 1"
                            select std.StandardID).FirstOrDefault()
                                select s;
    
    nestedQueries.ToList().ForEach(s => Console.WriteLine(s.StudentName));
  • 相关阅读:
    ASP.NET MVC5+EF6+EasyUI 后台管理系统--任务调度系统解析
    ueditor插入自定义内容和样式
    PHP跨页面传递时session失效
    apache 错误:The system cannot find the file specified.
    Thinkphp5 使用odbc连接到sqlserver
    thinkphp5在URL地址里隐藏模块名
    wamp设置自定义域名访问php网站
    wamp因配置错误而导致apache无法启动的问题
    Jquery 实现input回车时跳转到下一个input元素
    PHP ERROR : Call to undefined function curl_init()
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6616862.html
Copyright © 2020-2023  润新知