LINQ从列表中的列表中选择项目

时间:2018-04-18 08:31:57

标签: c# linq lambda

假设我的课程定义如下

public class Employee 
{
    public string Name { get; set;}
    public string Type { get; set; }
}
public class Dept
{
     public string Name { get; set;}
     public string Category { get; set; }
     public List<Employee> Employees { get; set;}
}

public class NewModel 
{
   public string Category { get; set;}
   public List<string> EmpNames { get; set;} 
}

如何将所有元素选择到新模型中,其中Employee类型为A,按类别分组?

新模型应该具有Category(来自Dept,它是分组键)和Employee Names列表。我已经完成了以下操作,但它没有给我我想要的东西。

var result =  Dept.Where(p =>p.Employees != null && p.Employees.Any(x => x.Type == 'A')).GroupBy(g => g.Category, (key,g) => new NewModel { Category = key, EmpNames = g.Select(p => p.Name).ToList()});

任何提示?

3 个答案:

答案 0 :(得分:1)

var departments = new List<Dept>(); // ? fill

var result = 
              departments.GroupBy(d => d.Category)
                         .Select(g => new NewModel
                                      {
                                          Category = g.Key,
                                          EmpNames = g.SelectMany(d => d.Employees)
                                                      .Where(e => e.Type == "A")   
                                                      .Select(e => e.Name) 
                                      });  

答案 1 :(得分:1)

这应该给你预期的结果: -

var result = depts.SelectMany(x => x.Employees.Where(z => z.Type == "A")
                           , (DeptObj, empObj) =>
                                                new
                                                {
                                                    DeptObj.Category,
                                                    empObj
                                                }
                                              ).GroupBy(x => x.Category)
                                              .Select(x => 
                        new NewModel 
                         { 
                             Category = x.Key, 
                             EmpNames = x.Select(z => z.empObj.Name).ToList() 
                         });

Sample Fiddle

答案 2 :(得分:0)

根据您的问题描述,不应该有Dept的单个对象,而是(需要)拥有dept对象列表。

在下面添加工作代码,

        List<Dept> deptList = new List<Dept>();
        List<Employee> empList = new List<Employee>();
        empList.Add(new Employee("d1" + "e1", "A"));
        empList.Add(new Employee("d1" + "e2", "B"));
        empList.Add(new Employee("d1" + "e3", "A"));
        deptList.Add(new Dept("D1", "D1C1", empList));

        empList = new List<Employee>();
        empList.Add(new Employee("d2" + "e1", "A"));
        empList.Add(new Employee("d2" + "e2", "B"));
        empList.Add(new Employee("d2" + "e3", "A"));
        deptList.Add(new Dept("D2", "D2C2", empList));

        empList = new List<Employee>();
        empList.Add(new Employee("d3" + "e1", "A"));
        empList.Add(new Employee("d3" + "e2", "B"));
        empList.Add(new Employee("d3" + "e3", "A"));
        deptList.Add(new Dept("D3", "D1C1", empList));

        List<NewModel> result = deptList
            .Where(p => p.Employees != null &&
                p.Employees
                .Any(x => x.Type == "A"))  //here this line is no more then just a check and can ignored
                .GroupBy(g => g.Category,
                (key, g) => new NewModel
                {
                    Category = key,
                    EmpNames = g.SelectMany(p => p.Employees.Where( x => x.Type == "A").Select(x => x.Name)).ToList()
                }).ToList();