Reputation: 1219
Have this models:
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; }
}
I need to create foreach loop for distinct Employee.Id Thanks for the advice.
Upvotes: 1
Views: 2651
Reputation: 236188
Assume you have vmDeliveryList
instance of your view model. With lambda syntax:
var ids = vmDeliveryList.ContractDeliveryList
.SelectMany(cdl => cdl.DeliveryList)
.Select(dl => dl.Employee.Id)
.Distinct();
Unfortunately there is no Distinct()
method in query syntax, so you can only select all ids, and then apply distinct:
var allIds = from cdl in vmDeliveryList.ContractDeliveryList
from dl in cdl.DeliverList
select dl.Employee.Id;
var ids = allIds.Distinct();
Upvotes: 1
Reputation: 13620
You can use SelectMany
to flatten the lists and then get the Id
with a Select
foreach(int id in viewModel.ContractDeliveryList.SelectMany(a => a.DeliveryList)
.Select(b => b.Employee.Id)
.Distinct() )
{
....
}
Upvotes: 3