首頁 > 軟體

LINQ操作符SelectMany的用法

2022-02-28 13:03:30

SelectMany操作符提供了將多個from子句組合起來的功能,相當於資料庫中的多表連線查詢,它將每個物件的結果合併成單個序列。

範例:

student類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SelectMany操作符
{
    /// <summary>
    /// 學生類
    /// </summary>
    public class Student
    {
        //姓名
        public string Name { get; set; }
        //成績
        public int Score { get; set; }
        //建構函式
        public Student(string name, int score)
        {
            this.Name = name;
            this.Score = score;
        }
    }
}

teacher類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SelectMany操作符
{
    /// <summary>
    /// Teacher類
    /// </summary>
    public class Teacher
    {
        //姓名
        public string Name { get; set; }
        //學生集合
        public List<Student> Students { get; set; }

        public Teacher(string name, List<Student> students)
        {
            this.Name = name;
            this.Students = students;
        }
    }
}

Program類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SelectMany操作符
{
    class Program
    {
        static void Main(string[] args)
        {
            //使用集合初始化器初始化Teacher集合
            List<Teacher> teachers = new List<Teacher> {
               new Teacher("徐老師",
               new List<Student>(){
                 new Student("宋江",80),
                new Student("盧俊義",95),
                new Student("朱武",45)
               }
               ),
                new Teacher("姜老師",
               new List<Student>(){
                 new Student("林沖",90),
                new Student("花榮",85),
                new Student("柴進",58)
               }
               ),
                new Teacher("樊老師",
               new List<Student>(){
                 new Student("關勝",100),
                new Student("阮小七",70),
                new Student("時遷",30)
               }
               )
            };

            //問題:查詢Score小於60的學生
            //方法1:迴圈遍歷、會有效能的損失
            foreach (Teacher t in teachers)
            {
                foreach (Student s in t.Students)
                {
                    if (s.Score < 60)
                    {
                        Console.WriteLine("姓名:" + s.Name + ",成績:"+s.Score);
                    }
                }
            }

            //查詢表示式
            //方法2:使用SelectMany  延遲載入:在不需要資料的時候,就不執行呼叫資料,能減輕程式和資料庫的互動,可以提供程式的效能,執行迴圈的時候才去存取資料庫取資料
            //直接返回學生的資料
            var query = from t in teachers
                        from s in t.Students
                        where s.Score < 60
                        select s;
            foreach (var item in query)
            {
                Console.WriteLine("姓名:" + item.Name + ",成績:"+item.Score);
            }
            //只返回老師的資料
            var query1 = from t in teachers
                         from s in t.Students
                         where s.Score < 60
                         select new {
                            t,
                            teacherName=t.Name,
                            student=t.Students.Where(p=>p.Score<60).ToList()
                         };
            foreach (var item in query1)
            {
                Console.WriteLine("老師姓名:" + item.teacherName + ",學生姓名:" +item.student.FirstOrDefault().Name+ ",成績:" + item.student.FirstOrDefault().Score);
            }
            // 使用匿名類 返回老師和學生的資料
            var query2 = from t in teachers
                         from s in t.Students
                         where s.Score < 60
                         select new { teacherName=t.Name, studentName=s.Name,studentScore=s.Score };
            foreach (var item in query2)
            {
                Console.WriteLine("老師姓名:" + item.teacherName + ",學生姓名:" + item.studentName + ",成績:" + item.studentScore);
            }

            //使用查詢方法
            var query3 = teachers.SelectMany(p => p.Students.Where(t=>t.Score<60).ToList());
            foreach (var item in query3)
            {
                Console.WriteLine("姓名:" + item.Name + ",成績:" + item.Score);
            }
            Console.ReadKey();

        }
    }
}

到此這篇關於LINQ操作符SelectMany的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com