日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

LINQ操作符SelectMany的用法_C#教程

作者:.NET開發(fā)菜鳥 ? 更新時(shí)間: 2022-05-01 編程語言

SelectMany操作符提供了將多個(gè)from子句組合起來的功能,相當(dāng)于數(shù)據(jù)庫中的多表連接查詢,它將每個(gè)對象的結(jié)果合并成單個(gè)序列。

示例:

student類:

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

namespace SelectMany操作符
{
    /// 
    /// 學(xué)生類
    /// 
    public class Student
    {
        //姓名
        public string Name { get; set; }
        //成績
        public int Score { get; set; }
        //構(gòu)造函數(shù)
        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操作符
{
    /// 
    /// Teacher類
    /// 
    public class Teacher
    {
        //姓名
        public string Name { get; set; }
        //學(xué)生集合
        public List Students { get; set; }

        public Teacher(string name, List 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 teachers = new List {
               new Teacher("徐老師",
               new List(){
                 new Student("宋江",80),
                new Student("盧俊義",95),
                new Student("朱武",45)
               }
               ),
                new Teacher("姜老師",
               new List(){
                 new Student("林沖",90),
                new Student("花榮",85),
                new Student("柴進(jìn)",58)
               }
               ),
                new Teacher("樊老師",
               new List(){
                 new Student("關(guān)勝",100),
                new Student("阮小七",70),
                new Student("時(shí)遷",30)
               }
               )
            };

            //問題:查詢Score小于60的學(xué)生
            //方法1:循環(huán)遍歷、會有性能的損失
            foreach (Teacher t in teachers)
            {
                foreach (Student s in t.Students)
                {
                    if (s.Score < 60)
                    {
                        Console.WriteLine("姓名:" + s.Name + ",成績:"+s.Score);
                    }
                }
            }

            //查詢表達(dá)式
            //方法2:使用SelectMany  延遲加載:在不需要數(shù)據(jù)的時(shí)候,就不執(zhí)行調(diào)用數(shù)據(jù),能減輕程序和數(shù)據(jù)庫的交互,可以提供程序的性能,執(zhí)行循環(huán)的時(shí)候才去訪問數(shù)據(jù)庫取數(shù)據(jù)
            //直接返回學(xué)生的數(shù)據(jù)
            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);
            }
            //只返回老師的數(shù)據(jù)
            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 + ",學(xué)生姓名:" +item.student.FirstOrDefault().Name+ ",成績:" + item.student.FirstOrDefault().Score);
            }
            // 使用匿名類 返回老師和學(xué)生的數(shù)據(jù)
            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 + ",學(xué)生姓名:" + 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();

        }
    }
}

原文鏈接:https://www.cnblogs.com/dotnet261010/p/6849980.html

欄目分類
最近更新