代わりに、ループ、コレクションの最初の関節問い合わせに割り当てられた値のためのLINQ SelectMany割り当て

    public class Employee
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public class EmployeeFile
    {
        public int EmployeeId { get; set; }
        public string FileName { get; set; }
    }

    public class EmployeeDTO
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FileName { get; set; }
    }

        static void Main(string[] args)
        {
             List<Employee> employees = new List<Employee>()
            {
             new Employee {Id = 1, FirstName   = "Joe", LastName    = "Rattz"},
             new Employee {Id = 2, FirstName   = "Willam", LastName = "Gates"},
             new Employee {Id = 3, FirstName   = "Anders", LastName = "Hejlsberg"},
             new Employee {Id = 4, FirstName   = "David", LastName  = "Lightman"},
             new Employee {Id = 101, FirstName = "Kevin", LastName  = "Flynn"}
            };


            List<EmployeeFile> employeeFiles = new List<EmployeeFile>()
            {
             new EmployeeFile {EmployeeId = 1,  FileName    = "001.txt"},
             new EmployeeFile {EmployeeId = 2,  FileName = "002.txt"},
             new EmployeeFile {EmployeeId = 3,  FileName = "003.txt"},
             new EmployeeFile {EmployeeId = 4,  FileName  = "004.txt"},
             new EmployeeFile {EmployeeId = 101,  FileName  = "005.txt"}
            };

            var result =
                            (from e in employees
                             join d in employeeFiles on e.Id equals d.EmployeeId
                             select new EmployeeDTO { Id = e.Id, FirstName = e.FirstName, LastName = e.LastName, FileName = null }).ToList();

            Console.WriteLine("第1个集合:");
            foreach (var item in result)
            {
                Console.WriteLine($"Id:{item.Id},FirstName:{item.FirstName},LastName:{item.LastName},FileName:{item.FileName}");
            }

            var objs = (from r in result
                        join d in employeeFiles on r.Id equals d.EmployeeId
                        select new EmployeeDTO
                        {
                            Id = r.Id,
                            FileName = d.FileName
                        }).ToList();

             Console.WriteLine("关联的集合:");
            foreach (var item in objs)
            {
                Console.WriteLine($"Id:{item.Id},FirstName:{item.FirstName},LastName:{item.LastName},FileName:{item.FileName}");
            }

            var objs2 = objs.SelectMany(d => result, (d, r) =>
            {
                EmployeeDTO dto = r;
                if (r.Id == d.Id)
                {
                    r.FileName = d.FileName;
                    return dto;
                }
                else
                {
                    dto = null;
                }

                return dto;

            }).Where(n => n != null).ToList();

             Console.WriteLine("合并2个集合:");
            foreach (var item in objs2)
            {
                Console.WriteLine($"Id:{item.Id},FirstName:{item.FirstName},LastName:{item.LastName},FileName:{item.FileName}");
            }
    }

おすすめ

転載: www.cnblogs.com/tangge/p/12635415.html