.NET(C#) Linq GroupBy和GroupJoin的使用

内容纲要

简介:

Linq是Language Integrated Query的简称,它是微软在.NET Framework 3.5里面新加入的特性,用以简化查询查询操作。

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;
        }
    }
}

给TA打赏
共{{data.count}}人
人已打赏
C#

C# File文件处理——删除文件

2022-10-28 12:45:09

C#

.NET(C#)字典(Dictionary)遍历方法

2022-10-31 11:18:38

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
有新私信 私信列表
搜索