如何迭代两个自定义对象列表并使用LINQ在C#中准备第三个?

时间:2018-04-04 03:30:14

标签: c# linq

我有以下2个列表。 EmployeesDto List,AccountsDto List。 从这两个列表中,我需要准备第三个列表:List。 为简单起见,我们假设EmployeesDto具有以下结构:

Class  EmployeesDto {
    public int EmpId {get; set; }
    public string EmpName {get; set; }
}
//And the AccountsDto is like:
Class  AccountsDto {
    public int EmpId {get; set; }
    public bool IsManager {get; set; }
}
// And LinkedEmployeesDto:
Class  LinkedEmployeesDto {
    public int EmpId {get; set; }
    public string EmpName {get; set; }
    public bool IsManager {get; set; }
}

现在我需要根据以上两个列表创建List,“IsManager'属性将从列表中设置,其中EmpId匹配列表和列表t。 我可以使用foreach来做到这一点,但是通过LINQ寻找更优雅的方式。

提前致谢。

4 个答案:

答案 0 :(得分:0)

您可以使用linq加入,以获得第三个列表

List<EmployeesDto> lstEmployeesDto = new List<EmployeesDto>();
        List<AccountsDto> lstAccountsDto = new List<AccountsDto>();

        var result = lstEmployeesDto.Join(lstAccountsDto,
                                   e => e.EmpId,
                                   d => d.EmpId, (employee, account) => new
                                   {
                                       EmpId = employee.EmpId,
                                       EmpName = employee.EmpName,
                                       IsManager = account.IsManager
                                   });

答案 1 :(得分:0)

如果您想避免加入,可以执行此操作。此示例假定您始终拥有相关数据以及Employees和Accounts之间的一对一关系。您可能希望添加自定义检查以确保。

List<EmployeesDto> Employees = new List<EmployeesDto>();
List<AccountsDto> Accounts = new List<AccountsDto>();

var linkedEmployees = Employees
                         .Select(e => 
                             new LinkedEmployeesDto
                                  { 
                                    EmpId = e.EmpId, 
                                    EmpName = e.EmpName,
                                    IsManager = Accounts
                                               .Single(a => a.EmpId == e.EmpId)
                                               .IsManager
                                  })
                         .ToList();

答案 2 :(得分:0)

根据您的需要,您可以使用以下两种表达方式。

    List<EmployeesDto> employees = new List<EmployeesDto>();
    employees.Add(new EmployeesDto { EmpId = 1, EmpName = "1" });
    employees.Add(new EmployeesDto { EmpId = 2, EmpName = "2" });
    employees.Add(new EmployeesDto { EmpId = 3, EmpName = "3" });
    employees.Add(new EmployeesDto { EmpId = 4, EmpName = "4" });

    List<AccountsDto> accounts = new List<AccountsDto>();
    accounts.Add(new AccountsDto { EmpId = 1, IsManager = true });
    accounts.Add(new AccountsDto { EmpId = 2, IsManager = true });


    // To get only those records which are in accounts list
    IEnumerable<LinkedEmployeesDto> linkedEmployees = employees.Join(accounts,
                                                                     employee => employee.EmpId,
                                                                     account => account.EmpId,
                                                                     (employee, account) => new LinkedEmployeesDto
                                                                     {
                                                                         EmpId = employee.EmpId,
                                                                         EmpName = employee.EmpName,
                                                                         IsManager = account.IsManager
                                                                     });

    // To get all the records from employee and set IsManager as false if record no in accounts list
    linkedEmployees = from employee in employees
                      join account in accounts on employee.EmpId equals account.EmpId into holder
                      from acc in holder.DefaultIfEmpty()
                      select new LinkedEmployeesDto
                      {
                          EmpId = employee.EmpId,
                          EmpName = employee.EmpName,
                          IsManager = (acc == null) ? false : acc.IsManager
                      };

答案 3 :(得分:0)

您可以使用LinQ中的Join来比较2个列表中的相等性并形成新列表。

List<EmployeesDto> Employees = new List<EmployeesDto>();            
        List<AccountsDto> Accounts = new List<AccountsDto>();           
        List<LinkedEmployeesDto> linkedEmployees = new List<LinkedEmployeesDto>();
        linkedEmployees = (from emp in Employees
                           join acc in Accounts
                                on emp.EmpId equals acc.EmpId
                           select new LinkedEmployeesDto()
                           {
                               EmpId = emp.EmpId,
                               EmpName = emp.EmpName,
                               IsManager = acc.IsManager
                           }).ToList();