简体   繁体   中英

LINQ select items from a list within a list

Assuming I have classes defined as below

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

How do I select all the elements into a new model which Employee type is A and group by Category?

The new model should have Category (from Dept which is grouped key) and list of Employee Names. I have done the below but it doesn't give me what I want.

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

Any hints?

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

This should give you expected result:-

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

As per your question description, there should not be single object of Dept , instead you will (need to) have List of dept object.

adding working code below,

        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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM