簡體   English   中英

LINQ從列表中的列表中選擇項目

[英]LINQ select items from a list within a list

假設我有如下定義的類

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並按類別分組的新模型中?

新模型應具有類別(來自部門的分組鍵)和雇員姓名列表。 我已經完成了下面的工作,但並沒有給我我想要的東西。

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

有什么提示嗎?

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

這應該給您預期的結果:

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

樣品小提琴

根據您的問題描述,不應有Dept單個對象,而是您將(需要)具有dept對象的List。

在下面添加工作代碼,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM