• LINQ 学习路程 -- 查询操作 Join


    Join操作是将两个集合联合

    Joining OperatorsUsage
    Join 将两个序列连接并返回结果集
    GroupJoin

    根据key将两个序列连接返回,像是SQL中的Left

    Join

    Join操作两个集合,inner collection 和 outer collection

    它返回一个集合(包含两个集合根据特定条件结合的所有元素),和SQL中的inner join一样

    public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer, 
                                                            IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, 
                                                            Func<TInner, TKey> innerKeySelector, 
                                                            Func<TOuter, TInner, TResult> resultSelector);
    
    public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer, 
                                                            IEnumerable<TInner> inner, 
                                                            Func<TOuter, TKey> outerKeySelector,
                                                            Func<TInner, TKey> innerKeySelector, 
                                                            Func<TOuter, TInner, TResult> resultSelector,
                                                            IEqualityComparer<TKey> comparer);
    IList<string> strList1 = new List<string>() { 
        "One", 
        "Two", 
        "Three", 
        "Four"
    };
    
    IList<string> strList2 = new List<string>() { 
        "One", 
        "Two", 
        "Five", 
        "Six"
    };
    
    var innerJoin = strList1.Join(strList2,
                          str1 => str1, 
                          str2 => str2, 
                          (str1, str2) => str1);
    public class Student{ 
        public int StudentID { get; set; }
        public string StudentName { get; set; }
        public int StandardID { get; set; }
    }
    
    public class Standard{ 
        public int StandardID { get; set; }
        public string StandardName { get; set; }
    }
    IList<Student> studentList = new List<Student>() { 
        new Student() { StudentID = 1, StudentName = "John", StandardID =1 },
        new Student() { StudentID = 2, StudentName = "Moin", StandardID =1 },
        new Student() { StudentID = 3, StudentName = "Bill", StandardID =2 },
        new Student() { StudentID = 4, StudentName = "Ram" , StandardID =2 },
        new Student() { StudentID = 5, StudentName = "Ron"  } 
    };
    
    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"}
    };
    
    var innerJoin = studentList.Join(// outer sequence 
                          standardList,  // inner sequence 
                          student => student.StandardID,    // outerKeySelector
                          standard => standard.StandardID,  // innerKeySelector
                          (student, standard) => new  // result selector
                                        {
                                            StudentName = student.StudentName,
                                            StandardName = standard.StandardName
                                        });

     在上面的例子中,studentList是外部序列,因为先从它开始查询,Join的第一个参数是指定的内部序列,第二个和第三个参数指定对应的字段

    外部序列的key选择器student => student.StandardID表明studentList中每个元素的StandardId字段必须与内部序列standard => standard.StandardID相对应

    如果所有的key值相等则被包含在结果集中

    最后一个参数是构建结果数据

     查询语法

    查询语法中的join和方法语法有些不同,它需要外部序列、内部序列、key选择器和结果选择器 

    from ... in outerSequence
    
    join ... in innerSequence  
    
    on outerKey equals innerKey
    
    select ...

    GroupJoin

    GroupJoin和Join是一样的除了GroupJoin返回一个Group(根据特定的group key)

    GroupJoin根据key联合两个序列并根据key分组

    public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer, 
    IEnumerable<TInner> inner,
    Func<TOuter, TKey> outerKeySelector,
    Func<TInner, TKey> innerKeySelector,
    Func<TOuter, IEnumerable<TInner>, TResult> resultSelector); public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer,
    IEnumerable<TInner> inner,
    Func<TOuter, TKey> outerKeySelector,
    Func<TInner, TKey> innerKeySelector,
    Func<TOuter, IEnumerable<TInner>,
    TResult> resultSelector,
    IEqualityComparer<TKey> comparer);
    IList<Student> studentList = new List<Student>() { 
        new Student() { StudentID = 1, StudentName = "John", StandardID =1 },
        new Student() { StudentID = 2, StudentName = "Moin", StandardID =1 },
        new Student() { StudentID = 3, StudentName = "Bill", StandardID =2 },
        new Student() { StudentID = 4, StudentName = "Ram",  StandardID =2 },
        new Student() { StudentID = 5, StudentName = "Ron" } 
    };
    
    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"}
    };
    
    var groupJoin = standardList.GroupJoin(studentList,  //inner sequence
                                    std => std.StandardID, //outerKeySelector 
                                    s => s.StandardID,     //innerKeySelector
                                    (std, studentsGroup) => new // resultSelector 
                                    {
                                        Students = studentsGroup,
                                        StandarFulldName = std.StandardName
                                    });
    
    foreach (var item in groupJoin)
    { 
        Console.WriteLine(item.StandarFulldName );
    
        foreach(var stud in item.Students)
            Console.WriteLine(stud.StudentName);
    }

     查询语法

    from ... in outerSequence
    
    join ... in innerSequence  
    
    on outerKey equals innerKey
    
    into groupedCollection    
    
    select ...
  • 相关阅读:
    大话串口:我和它的恩恩怨怨
    分布式网游server的一些想法(一) 语言和平台的选择
    C++: C没有闭包真的很痛苦……
    C++不是C/C++
    最美树算法
    类魔兽世界 技能 天赋 成就 log 系统设计
    C++网游服务端开发(一):又无奈的重复造了个轮子,一个底层网络库
    C++ protobuf 不仅仅是序列化……
    深入WPFStyle
    Illusion = Caliburn.Micro + MEF
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6602708.html
Copyright © 2020-2023  润新知