最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

LINQ操作符SelectMany的用法

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

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

示例:

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("時(shí)遷",30)
               }
               )
            };

            //問題:查詢Score小于60的學(xué)生
            //方法1:循環(huán)遍歷、會(huì)有性能的損失
            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();

        }
    }
}

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

相關(guān)文章

  • C#查找字符串所有排列組合的方法

    C#查找字符串所有排列組合的方法

    這篇文章主要介紹了C#查找字符串所有排列組合的方法,涉及C#字符串操作的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • Unity3d使用FairyGUI 自定義字體的操作

    Unity3d使用FairyGUI 自定義字體的操作

    由于本項(xiàng)目ui使用了第三方ui系統(tǒng) fairyGUI 所以此文章主要講述unity3d和fairygui搭配使用字體的過程
    2021-04-04
  • 詳解如何利用C#實(shí)現(xiàn)漢字轉(zhuǎn)拼音功能

    詳解如何利用C#實(shí)現(xiàn)漢字轉(zhuǎn)拼音功能

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)漢字轉(zhuǎn)拼音的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12
  • C#讀取本地網(wǎng)絡(luò)配置信息的方法小結(jié)

    C#讀取本地網(wǎng)絡(luò)配置信息的方法小結(jié)

    在現(xiàn)代軟件開發(fā)中,處理網(wǎng)絡(luò)配置信息是一個(gè)常見需求,無論是開發(fā)桌面、移動(dòng)還是服務(wù)器應(yīng)用程序,了解如何在C#中讀取和管理網(wǎng)絡(luò)配置信息都是非常有用的,本文將探討在C#中讀取本地網(wǎng)絡(luò)配置信息的方法,并提供幾個(gè)實(shí)際應(yīng)用場(chǎng)景的示例,需要的朋友可以參考下
    2024-10-10
  • 詳解C# Lazy Loading(延遲加載)

    詳解C# Lazy Loading(延遲加載)

    這篇文章主要介紹了C# Lazy Loading(延遲加載)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • C#實(shí)現(xiàn)最完整的文件和目錄操作類實(shí)例

    C#實(shí)現(xiàn)最完整的文件和目錄操作類實(shí)例

    這篇文章主要介紹了C#實(shí)現(xiàn)最完整的文件和目錄操作類,涉及C#針對(duì)文件與目錄的創(chuàng)建、獲取、檢測(cè)、刪除等常用操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-05-05
  • C#使用ImitateLogin模擬登錄百度

    C#使用ImitateLogin模擬登錄百度

    這篇文章主要介紹了C#使用ImitateLogin模擬登錄百度 的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • 六大設(shè)計(jì)原則之開閉原則

    六大設(shè)計(jì)原則之開閉原則

    這篇文章介紹了六大設(shè)計(jì)原則之開閉原則,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • C#基于共享內(nèi)存實(shí)現(xiàn)跨進(jìn)程隊(duì)列

    C#基于共享內(nèi)存實(shí)現(xiàn)跨進(jìn)程隊(duì)列

    進(jìn)程通信一般情況下比較少用,但是也有一些使用場(chǎng)景,有些做視頻傳輸?shù)乃坪鯐?huì)用多進(jìn)程來實(shí)現(xiàn),還有在子進(jìn)程中調(diào)用特定的庫來避免內(nèi)存泄漏,筆者最近也遇到了需要使用多進(jìn)程的場(chǎng)景,本文介紹了C#基于共享內(nèi)存實(shí)現(xiàn)跨進(jìn)程隊(duì)列,需要的朋友可以參考下
    2024-07-07
  • C#遍歷List并刪除某個(gè)元素的方法

    C#遍歷List并刪除某個(gè)元素的方法

    這篇文章主要介紹了C#遍歷List并刪除某個(gè)元素的方法,實(shí)例分析了正序與倒序遍歷list及刪除元素的使用技巧,需要的朋友可以參考下
    2015-02-02

最新評(píng)論

九龙县| 醴陵市| 泗阳县| 察隅县| 璧山县| 晋中市| 泰宁县| 喀喇沁旗| 衡水市| 长岛县| 宝鸡市| 龙游县| 信阳市| 江川县| 邢台县| 东台市| 沂南县| 德保县| 龙胜| 宁河县| 建湖县| 桐城市| 徐闻县| 德庆县| 东至县| 绥棱县| 陵水| 长春市| 鹤峰县| 平远县| 罗江县| 封丘县| 阜宁县| 镇巴县| 津市市| 靖西县| 沾益县| 高青县| 凤台县| 吉隆县| 砀山县|