星期一, 10月 08, 2018

[LINQ] SelectMany 應用 - Cross Join

利用 SelectMany 來達成 TSQL Cross Join 效果
namespace CrossJoinSample
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] Departments = { "研發" , "會計" , "人事" , "資訊" };
            string[] Employees = { "張三" , "李四" , "王五" };

            var result = Departments.SelectMany(
                e => Employees, 
                (d, e) => new {
                    Department = d ,
                    Employees = e
                });

            foreach (var item in result)
            {
                Console.WriteLine($"{item.Department} - {item.Employees}");
            }
        }
    }
}

[LINQ] Cross Join

沒有留言:

張貼留言