zoukankan      html  css  js  c++  java
  • LINQ SelectMany代替for循环赋值,把联合查询的值赋值给第1个集合

        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}");
                }
        }
    
  • 相关阅读:
    css添加方法
    node + vue 实现服务端单向推送消息,利用EventSource
    获取公众号openid,通过unionid 和小程序用户绑定起来
    小程序 构建npm
    powershell禁止系统运行脚本
    mongoose 删除
    mongoose 查询
    moogoose 更新
    小程序,用户授权手机号,node需要检验和解析
    小程序:支付的时候缺少参数:total_fee,支付失败
  • 原文地址:https://www.cnblogs.com/tangge/p/12635415.html
Copyright © 2011-2022 走看看