C#中Linq GroupBy和GroupJoin的使用

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

 

1、GroupBy操作符

GroupBy操作符类似于SQL语言仲的Gruop By语句,这里的GroupBy操作符用于将输入序列中的元素进行分组。

例如,

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);
           var pList1 = pList.GroupBy(p=>p.Age);      
           //var pList1 = pList.GroupBy(p => new { Id=p.Id,Age=p.Age,Name=p.Name });//指定多个字段,可以去重
            foreach (var item in pList1)
            {
                Console.Write(item.Key);   
                foreach (var item1 in item)
                {
                    Console.Write(item1.Name);      
                }
                Console.WriteLine();
            }
            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;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忧郁的蛋~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值