Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 嵌套列表中的不同项_C#_List_Distinct Values - Fatal编程技术网

C# 嵌套列表中的不同项

C# 嵌套列表中的不同项,c#,list,distinct-values,C#,List,Distinct Values,有以下型号: public class VMDeliveryList //[View-model] { public List<ContractDelivery> ContractDeliveryList { get; set; } } public class ContractDelivery { public Contract Contract { get; set; } public List<Delivery> DeliveryList

有以下型号:

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循环 谢谢您的建议。

假设您有视图模型的vmDeliveryList实例。使用lambda语法:

var ids = vmDeliveryList.ContractDeliveryList
                        .SelectMany(cdl => cdl.DeliveryList)
                        .Select(dl => dl.Employee.Id)
                        .Distinct();
遗憾的是,查询语法中没有Distinct方法,因此只能选择所有ID,然后应用Distinct:

var allIds = from cdl in vmDeliveryList.ContractDeliveryList
             from dl in cdl.DeliverList
             select dl.Employee.Id;

var ids = allIds.Distinct();
假设您有视图模型的vmDeliveryList实例。使用lambda语法:

var ids = vmDeliveryList.ContractDeliveryList
                        .SelectMany(cdl => cdl.DeliveryList)
                        .Select(dl => dl.Employee.Id)
                        .Distinct();
遗憾的是,查询语法中没有Distinct方法,因此只能选择所有ID,然后应用Distinct:

var allIds = from cdl in vmDeliveryList.ContractDeliveryList
             from dl in cdl.DeliverList
             select dl.Employee.Id;

var ids = allIds.Distinct();
您可以使用来展平列表,然后通过选择获取Id

您可以使用来展平列表,然后通过选择获取Id


使用Distinct关键字和LINQuse Distinct关键字和LINQThanks,我不太熟悉SelectMany。对我来说,正确的方法是:Model.contracteddeliverylist.SelectManyx=>x.DeliveryList.Selectx=>x.Employee.Id.DistinctThanks,我对SelectMany不太熟悉。对我来说,正确的方法是:Model.contracteddeliverylist.SelectManyx=>x.DeliveryList.Selectx=>x.Employee.Id.Distinct