星期三, 10月 10, 2018

[LINQ] GroupJoin

根據 MSDN 文章 - 執行左方外部聯結 的 GroupJoin 練習
namespace GroupJoinSample
{
    class Program
    {
        static void Main(string[] args)
        {
            var Data = DemoHelper.GetData();
            var People = Data.People;
            var Pets = Data.Pets;
            
            var LINQResult = People
                .GroupJoin(
                    Pets,
                    pe => pe,
                    pt => pt.Owner,
                    (personArg, petArg) => new { PersonProp = personArg, PetsProp = petArg });

            Console.WriteLine("----- LINQ GroupJoin 結果 -----");
            foreach (var person in LINQResult)
            {
                Console.WriteLine($"飼主:{person.PersonProp.FirstName} 有 {person.PetsProp.Count()} 隻寵物");

                foreach (var pet in person.PetsProp)
                {
                    Console.WriteLine($"--- 寵物名稱:{pet.Name}");
                }

                Console.WriteLine("");
            }

            Console.WriteLine("----- TSQL Left Join 結果 -----");

            var TSQLResult = People
                .GroupJoin(
                    Pets,
                    pe => pe,
                    pt => pt.Owner,
                    (personArg, petArg) => new { PersonProp = personArg, PetsProp = petArg })
                .SelectMany(
                    p => p.PetsProp.DefaultIfEmpty(), 
                    (pe, pt) => new {
                                        OwnerName = $"{pe.PersonProp.FirstName}",
                                        // 傳統寫法
                                        // PetName = pt == null ? "無寵物" : pt.Name
                                        // 語法糖寫法
                                        PetName = pt?.Name ?? "無寵物"
                                    });

            Console.WriteLine("飼主_寵物名字");
            foreach (var Record in TSQLResult)
            {
                Console.WriteLine(Record.OwnerName + "_" + Record.PetName);
            }

        }
    }

    public class DemoHelper
    {
        public static (List<Person> People, List<Pet> Pets) GetData()
        {
            Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund" };
            Pet daisy = new Pet { Name = "Daisy", Owner = magnus };

            Person terry = new Person { FirstName = "Terry", LastName = "Adams" };
            Pet barley = new Pet { Name = "Barley", Owner = terry };
            Pet boots = new Pet { Name = "Boots", Owner = terry };
            Pet bluemoon = new Pet { Name = "Blue Moon", Owner = terry };

            Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss" };
            Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };

            // 該員沒有飼養寵物
            Person arlene = new Person { FirstName = "Arlene", LastName = "Huff" };

            List<Person> people = new List<Person> { magnus, terry, charlotte, arlene };
            List<Pet> pets = new List<Pet> { barley, boots, whiskers, bluemoon, daisy };

            return (people, pets);
        }
    }

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public class Pet
    {
        public string Name { get; set; }
        public Person Owner { get; set; }
    }
}
[LINQ] GroupJoin

沒有留言:

張貼留言