Linq是Language Integrated Query的简称,它是微软在.NET Framework 3.5里面新加入的特性,用以简化查询查询操作。本文主要介绍.NET(C#) 中Linq的Join和GroupJoin操作符。

1、Join操作符

Join操作符类似于SQL语句中的Join语句用于连接多个表,Linq to OBJECT中Join操作符可以用来连接两个输入序列。

例如,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            List<People> pList = new List<People>();
            People p1 = new People(1, "C", 4);
            People p2 = new People(2, "Java", 7);
            People p3 = new People(3, "Python", 11);
            People p4 = new People(4, "Linux", 15);
            People p5 = new People(5,"CJavaPY",1);
            pList.Add(p1);
            pList.Add(p2);
            pList.Add(p3);
            pList.Add(p4);
            pList.Add(p5);
            List<Record> rList = new List<Record>();
            Record r1 = new Record(1, 3);
            Record r2 = new Record(2, 5);
            Record r3 = new Record(3, 7);
            Record r4 = new Record(4, 20);
            Record r5 = new Record(5, 11);
            rList.Add(r1);
            rList.Add(r2);
            rList.Add(r3);
            rList.Add(r4);
            rList.Add(r5);
            var Items = pList.Join(rList, p => p.Id, r => r.PId, (p, r) => new { Name = p.Name, WarRecord = r.WarRecord });
            foreach (var item in Items)
            {
                Console.WriteLine(item.Name + ":" + item.WarRecord);          
            }
            Console.ReadKey();
        }
    }
    public class People
    {
        public People(int id, string name, int age)
        {
            this.Id = id;
            this.Name = name;
            this.Age = age;
        }
        public int Id
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
    }
    public class Record
    {
        public Record(int id, int warRecord)
        {
            this.PId = id;
            this.WarRecord = warRecord;
        }
        public int PId
        {
            get;
            set;
        }
        public int WarRecord
        {
            get;
            set;
        }
    }
}

2、GroupJoin操作符

GroupJoin操作符也用于连接两个输入序列,但与Join操作符不同稍有不同,Join操作符在列举outer序列元素时,会将一个outer序列元素和其对应的inner序列元素作为一组参数传递给委托resultSelector委托,也就是如果某一个outer序列元素有多个对应的inner序列元素,Join操作符将会分多次将outer序列元素和每一个对应的inner序列元素传递给委托resultSelector。使用GroupJoin操作符时,如果某一个outer序列元素有多个对应的inner序列元素,那么这多个对应的inner序列元素会作用一个序列一次性传递给委托resultSelecotr,可以针对此序列添加一些处理逻辑。

例如,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            List<People> pList = new List<People>();
            People p1 = new People(1, "C", 4);
            People p2 = new People(2, "Java", 7);
            People p3 = new People(3, "Python", 11);
            People p4 = new People(4, "Linux", 15);
            People p5 = new People(5,"CJavaPY",1);
            pList.Add(p1);
            pList.Add(p2);
            pList.Add(p3);
            pList.Add(p4);
            pList.Add(p5);
            List<Record> rList = new List<Record>();
            Record r1 = new Record(1, 3);
            Record r2 = new Record(2, 5);
            Record r3 = new Record(3, 7);
            Record r4 = new Record(4, 20);
            Record r5 = new Record(1, 11);
            rList.Add(r1);
            rList.Add(r2);
            rList.Add(r3);
            rList.Add(r4);
            rList.Add(r5);
            var Items = pList.Join(rList, p => p.Id, r => r.PId, (p, r) => new { Name = p.Name, WarRecord = r.WarRecord });
            foreach (var item in Items)
            {
                Console.WriteLine(item.Name + ":" + item.WarRecord);
            }
            Console.WriteLine();
            var Items1 = pList.GroupJoin(rList, p => p.Id, r => r.PId, (p, List1) => new { Name = p.Name, WarRecords = List1.Sum(r=>r.WarRecord) });
            //Join与GrouyJoin的不同,Join每次都会传递一个元素到输出序列,而GruopJoin会将相同序列序号作为一个集合的形式传递给输出委托    
            foreach (var item in Items1)
            {
                Console.WriteLine(item.Name + ":" + item.WarRecords); 
            }
        }
    }
    public class People
    {
        public People(int id, string name, int age)
        {
            this.Id = id;
            this.Name = name;
            this.Age = age;
        }
        public int Id
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
    }
    public class Record
    {
        public Record(int id, int warRecord)
        {
            this.PId = id;
            this.WarRecord = warRecord;
        }
        public int PId
        {
            get;
            set;
        }
        public int WarRecord
        {
            get;
            set;
        }
    }
}

推荐文档