欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

LINQ操作符SelectMany的用法

 更新時間:2022年02月28日 11:25:05   作者:.NET開發(fā)菜鳥  
這篇文章介紹了LINQ操作符SelectMany的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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

示例:

student類:

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

namespace SelectMany操作符
{
    /// <summary>
    /// 學(xué)生類
    /// </summary>
    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操作符
{
    /// <summary>
    /// Teacher類
    /// </summary>
    public class Teacher
    {
        //姓名
        public string Name { get; set; }
        //學(xué)生集合
        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("柴進(jìn)",58)
               }
               ),
                new Teacher("樊老師",
               new List<Student>(){
                 new Student("關(guān)勝",100),
                new Student("阮小七",70),
                new Student("時遷",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ù)的時候,就不執(zhí)行調(diào)用數(shù)據(jù),能減輕程序和數(shù)據(jù)庫的交互,可以提供程序的性能,執(zhí)行循環(huán)的時候才去訪問數(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();

        }
    }
}

到此這篇關(guān)于LINQ操作符SelectMany的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#使用OpenCvSharp實現(xiàn)透視變換功能

    C#使用OpenCvSharp實現(xiàn)透視變換功能

    這篇文章主要為大家詳細(xì)介紹了C#如何使用OpenCvSharp實現(xiàn)透視變換的功能,文中的示例代碼簡潔易懂,具有一定的學(xué)習(xí)價值,需要的小伙伴可以參考下
    2023-11-11
  • C#實現(xiàn)根據(jù)給出的相對地址獲取網(wǎng)站絕對地址的方法

    C#實現(xiàn)根據(jù)給出的相對地址獲取網(wǎng)站絕對地址的方法

    這篇文章主要介紹了C#實現(xiàn)根據(jù)給出的相對地址獲取網(wǎng)站絕對地址的方法,涉及C#URL及字符串操作的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • unity將圖片轉(zhuǎn)換成字體的方法

    unity將圖片轉(zhuǎn)換成字體的方法

    這篇文章主要為大家詳細(xì)介紹了unity將圖片轉(zhuǎn)換成字體的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C#_SqlDependency的使用詳解

    C#_SqlDependency的使用詳解

    SqlDependency對象表示應(yīng)用程序和 SQL Server 實例間的查詢通知依賴關(guān)系,這篇文章主要介紹了C#_SqlDependency的使用,需要的朋友可以參考下
    2023-06-06
  • C#實現(xiàn)簡單的計算器小程序

    C#實現(xiàn)簡單的計算器小程序

    這篇文章主要為大家詳細(xì)介紹了C#實現(xiàn)簡單的計算器小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C#如何控制IIS動態(tài)添加刪除網(wǎng)站詳解

    C#如何控制IIS動態(tài)添加刪除網(wǎng)站詳解

    這篇文章主要給大家介紹了關(guān)于C#如何控制IIS動態(tài)添加刪除網(wǎng)站的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • c#實現(xiàn)數(shù)據(jù)同步的方法(使用文件監(jiān)控對象filesystemwatcher)

    c#實現(xiàn)數(shù)據(jù)同步的方法(使用文件監(jiān)控對象filesystemwatcher)

    這篇文章主要介紹了C#使用文件監(jiān)控對象FileSystemWatcher實現(xiàn)數(shù)據(jù)同步,大家參考使用吧
    2013-12-12
  • C#類中屬性與成員變量的使用小結(jié)

    C#類中屬性與成員變量的使用小結(jié)

    本篇文章主要是對C#類中屬性與成員變量的使用進(jìn)行了總結(jié)介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • C#正則表達(dá)式大全

    C#正則表達(dá)式大全

    本文詳細(xì)講解了C#正則表達(dá)式的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#托管堆對象實例包含內(nèi)容分析

    C#托管堆對象實例包含內(nèi)容分析

    這篇文章主要介紹了C#托管堆對象實例包含內(nèi)容,實例展示了托管對象的結(jié)構(gòu)及運行原理,需要的朋友可以參考下
    2014-09-09

最新評論