LINQ之Join(from join on )与GroupJoin(from join on into)将两个集合进行关联与分组
[C#] LINQ之Join与GroupJoin - cnxy - 博客园 (cnblogs.com)
[C#]使用Join与GroupJoin将两个集合进行关联与分组 - cnxy - 博客园 (cnblogs.com)
LINQ TO SQL 中的join - min.jiang - 博客园 (cnblogs.com)
Enumerable.Join 方法 (System.Linq) | Microsoft Docs
Enumerable.GroupJoin 方法 (System.Linq) | Microsoft Docs
代码1如下
void Main() { Person[] persons = new Person[] { new Person{ CityID = 1, Name = "ABC" }, new Person{ CityID = 1, Name = "EFG" }, new Person{ CityID = 2, Name = "HIJ" }, new Person{ CityID = 3, Name = "KLM" }, new Person{ CityID = 3, Name = "NOP" }, new Person{ CityID = 4, Name = "QRS" }, new Person{ CityID = 5, Name = "TUV" } }; City[] cities = new City[] { new City{ ID = 1,Name = "Guangzhou" }, new City{ ID = 2,Name = "Shenzhen" }, new City{ ID = 3,Name = "Beijing" }, new City{ ID = 4,Name = "Shanghai" } }; Console.WriteLine("********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); //官方释义:基于匹配键对两个序列的元素进行关联。使用默认的相等比较器对键进行比较。 var result1 = persons.Join(cities, p => p.CityID, c => c.ID, (p, c) => new { PersonName = p.Name, CityName = c.Name }); // 其等价的LINQ语句为: var result = from p in persons join c in cities on p.CityID equals c.ID select new { PersonName = p.Name, CityName = c.Name }; foreach (var item in result1) { Console.WriteLine($"{item.PersonName},{item.CityName}"); } Console.WriteLine(); Console.WriteLine("********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, IEqualityComparer<TKey> comparer); //官方释义:基于匹配键对两个序列的元素进行关联。使用指定的IEqualityComparer<TKey> 对键进行比较。 Console.WriteLine("********GroupJoin第一种方法*************************************"); //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); //官方释义: 基于键相等对两个序列的元素进行关联并对结果进行分组。使用默认的相等比较器对键进行比较。 var result3 = persons.GroupJoin(cities, p => p.CityID, c => c.ID, (p, cs) => new { PersonName = p.Name, Citys = cs }); // 其等价的LINQ语句为: //var result3 = from p in persons // join c in cities on p.CityID equals c.ID into cs // select new { PersonName = p.Name, Citys = cs }; foreach (var item in result3) { Console.Write($"{item.PersonName}\t"); foreach (var city in item.Citys) { Console.Write($"{city.Name}"); } Console.WriteLine(); } Console.WriteLine(); //使用cities去左关联persons var result4 = cities.GroupJoin(persons, p => p.ID, c => c.CityID, (p, cs) => new { PersonName = p.Name, Persons = cs }); foreach (var item in result4) { Console.Write($"{item.PersonName}:"); foreach (var city in item.Persons) { Console.Write($"{city.Name}\t"); } Console.WriteLine(); } Console.WriteLine(); Console.WriteLine("********GroupJoin第二种方法*************************************"); //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); //官方释义:基于键相等对两个序列的元素进行关联并对结果进行分组。使用指定的IEqualityComparer<TKey> 对键进行比较。 } class Person { public int CityID { set; get; } public string Name { set; get; } } class City { public int ID { set; get; } public string Name { set; get; } }
结果1:
********Join第一种用法************************************* ABC,Guangzhou EFG,Guangzhou HIJ,Shenzhen KLM,Beijing NOP,Beijing QRS,Shanghai ********Join第二种用法************************************* ********GroupJoin第一种方法************************************* ABC Guangzhou EFG Guangzhou HIJ Shenzhen KLM Beijing NOP Beijing QRS Shanghai TUV Guangzhou:ABC EFG Shenzhen:HIJ Beijing:KLM NOP Shanghai:QRS ********GroupJoin第二种方法*************************************
代码2如下
void Main() { List<Person> pList = new List<Person> { new Person(){ Name = "ABC", Age = 18 }, new Person(){ Name = "EFG", Age = 19 }, new Person(){ Name = "LMN", Age = 20 }, new Person(){ Name = "XYZ", Age = 21 } }; List<Department> dList = new List<Department> { new Department() { Name = "A1", Employee = pList[0] }, new Department() { Name = "A2", Employee = pList[1] }, new Department() { Name = "A3", Employee = pList[0] }, new Department() { Name = "B1", Employee = pList[2] }, new Department() { Name = "B2", Employee = pList[3] }, new Department() { Name = "B3", Employee = pList[3] } }; Console.WriteLine("********Join方法*************************************"); var result = pList.Join(dList, person => person, department => department.Employee, (person, department) => new { Person = person, Department = department }); foreach (var item1 in result) { Console.WriteLine($"Name:{item1.Person} & Department:{item1.Department} "); } Console.WriteLine(); Console.WriteLine("********GroupJoin方法*************************************"); var result2 = pList.GroupJoin(dList, person => person, department => department.Employee, (person, departments) => new { Person = person, Department = departments.Select(d => d) }); foreach (var item1 in result2) { Console.WriteLine($"Name:{item1.Person} & "); foreach (var item2 in item1.Department) { if (item1.Department.First() == item2) Console.Write($"Department:{item2} "); else Console.Write($"{item2} "); } } } class Person { public string Name { set; get; } public int Age { set; get; } public override string ToString() { return $"{Name},{Age}"; } } class Department { public string Name { set; get; } public Person Employee { set; get; } public override string ToString() { return $"{Name}"; } }
结果:
********Join方法************************************* Name:ABC,18 & Department:A1 Name:ABC,18 & Department:A3 Name:EFG,19 & Department:A2 Name:LMN,20 & Department:B1 Name:XYZ,21 & Department:B2 Name:XYZ,21 & Department:B3 ********GroupJoin方法************************************* Name:ABC,18 & Department:A1 A3 Name:EFG,19 & Department:A2 Name:LMN,20 & Department:B1 Name:XYZ,21 & Department:B2 B3
posted on 2021-07-12 15:07 springsnow 阅读(679) 评论(0) 编辑 收藏 举报