Skip to main content
 首页 » 编程设计

c#之嵌套列表中的不同项

2024年02月06日16arxive

有这个模型:

public class VMDeliveryList  //[View-model] 
{ 
    public List<ContractDelivery> ContractDeliveryList { get; set; } 
} 
 
public class ContractDelivery 
{ 
    public Contract Contract { get; set; } 
    public List<Delivery> DeliveryList { get; set; } 
} 
 
public class Delivery 
{ 
    public int Id { get; set; } 
    public Employee Employee { get; set; } 
} 
 
public class Employee 
{ 
    public int Id { get; set; } 
} 

我需要为不同的 Employee.Id 创建 foreach 循环 感谢您的建议。

请您参考如下方法:

您可以使用 SelectMany展平列表,然后使用 Select

获取 Id
foreach(int id in viewModel.ContractDeliveryList.SelectMany(a => a.DeliveryList) 
                                                .Select(b => b.Employee.Id) 
                                                .Distinct() ) 
{ 
    .... 
}