Enumerable<T>的GroupJoin 方法和Join的用法和区别

平时在项目中很多时候直接用 from s in list1 join s2 in list2 ....这样的写法,最近在项目中看到有同事用了Enumerable<T>的GroupJoin 方法和Join的用法,我也没有深究二者的区别,今天就做了一个总结。

首先我们 先看join 的用法:

 1  class Person
 2     {
 3         public string Name { get; set; }
 4     }
 5 
 6     class Pet
 7     {
 8         public string Name { get; set; }
 9         public Person Owner { get; set; }
10     }
11     class Program
12     {
13         private static readonly int[] nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
14         static void Main(string[] args)
15         {
28             Person magnus = new Person { Name = "Hedlund, Magnus" };
29             Person terry = new Person { Name = "Adams, Terry" };
30             Person charlotte = new Person { Name = "Weiss, Charlotte" };
31 
32             Pet barley = new Pet { Name = "Barley", Owner = terry };
33             Pet boots = new Pet { Name = "Boots", Owner = terry };
34             Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
35             Pet daisy = new Pet { Name = "Daisy", Owner = new Person() };
36 
37             List<Person> people = new List<Person> { magnus, terry, charlotte };
38             List<Pet> pets = new List<Pet> { barley, boots, whiskers, daisy };
65             var query = people.Join
66                    (pets,
67                     person => person,
68                     pet => pet.Owner,
69                     (person, pet) =>
70                         new
71                         {
72                             OwnerName = person.Name,
73                             Pet = pet.Name
74                         });
75 
76             foreach (var obj in query)
77             {
78                 Console.WriteLine(
79                     "{0} - {1}",
80                     obj.OwnerName,
81                     obj.Pet);
82             }
83             Console.ReadKey();
84         }

我们断点调试的时候可以看到:

 

控制台输出的结果:

 

从上面的结果我们可以看出Join是一个inner join的结果,同时这个结果集是一个一维的平面结果集。

还是相同的数据,我们现在把代码改成GroupJoin的写法如下:

            var query =
                people.GroupJoin(pets,
                                 person => person,
                                 pet => pet.Owner,
                                 (person, petCollection) =>
                                     new
                                     {
                                         OwnerName = person.Name,
                                         Pets = petCollection.Select(pet => pet.Name),
                                         Count = petCollection.Count()
                                     });

            foreach (var obj in query)
            {
                // Output the owner's name.
                Console.WriteLine("{0}:", obj.OwnerName);
                // Output each of the owner's pet's names.
                foreach (string pet in obj.Pets)
                {
                    Console.WriteLine("  {0}", pet);
                }
            }
            Console.ReadKey();

我们调试的时候可以看到如下:

 

 

再看看输出的结果

 

我们可以看出GroupJoin是一个left join的结果,是一个分组后的二维结果集

 

posted @ 2020-11-05 11:44  Tim1027  阅读(118)  评论(0编辑  收藏  举报