首页 > 其他 > 详细

LINQ SelectMany代替for循环赋值,把联合查询的值赋值给第1个集合

时间:2020-04-05 01:55:27      阅读:198      评论:0      收藏:0      [点我收藏+]

技术分享图片

    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}");
            }
    }

LINQ SelectMany代替for循环赋值,把联合查询的值赋值给第1个集合

原文:https://www.cnblogs.com/tangge/p/12635415.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!