Lambda with nested classes

I posted this question a while ago, but got a partial answer to my problem, so I decided to post more explanations, hoping to get a more accurate answer. I have 2 classes:

public class Employee
{
    public string Name { get; set; }
    public List<Cars> Cars { get; set; }
}

public class Car
{
    public int CarID { get; set; }
    public CarTypes CarType { get; set; }
    public enum CarTypes
    {
        Van,
        SmallCar
    }
}

I am trying to get only all the employees who have allocated minibuses to ignore those who work with SmallCars using Lambda, I tried this line:

List<Employee> EmployeesWithVans = AllEmployees.Where(emps => emps.Car.Any(cartype => cartype.CarType == Car.CarTypes.Van)).ToList();

But this gets all the employees if at least one van is distributed to the employee ( .Any), if I try ( .All), he will not return anything, since not all employees have a van.

Any idea if this can be achieved using a nested Lambda?

Thank.

Edit:

Employee Mark = new Employee();
Mark.Cars.Add(new Car() { CarType = Car.CarTypes.Van, CarID = 12 });
Mark.Cars.Add(new Car() { CarType = Car.CarTypes.Van, CarID = 13 });
Mark.Cars.Add(new Car() { CarType = Car.CarTypes.SmallCar, CarID = 14 });

Employee Lisa = new Employee();
Lisa.Cars.Add(new Car() { CarType = Car.CarTypes.Van, CarID = 15 });
Lisa.Cars.Add(new Car() { CarType = Car.CarTypes.SmallCar, CarID = 16 });
Lisa.Cars.Add(new Car() { CarType = Car.CarTypes.SmallCar, CarID = 17 });

    List<Employee> EmployeesWithVans should contain:

    Employee FilteredMark contains:
    Mark.Cars.Add(new Car() { CarType = Car.CarTypes.Van, CarID = 12 });
    Mark.Cars.Add(new Car() { CarType = Car.CarTypes.Van, CarID = 13 });

    Employee FilteredLisa contains:
    Mark.Cars.Add(new Car() { CarType = Car.CarTypes.Van, CarID = 15 });
+5
3

:

List<Employee> Temp = AllEmployees.Where(emps => emps.Cars.Any(cartype => cartype.CarType == Cars.CarTypes.Van)).ToList();

List<Employee> EmployeesWithVans = (from item in Temp
           select new Employee{ 
                                     Name = item.Name, 
                                     Cars = (item.Cars.Where( car => car.CarType == Cars.CarTypes.Van)).ToList()
                               }).ToList();

, ( LINQPAD):

void Main()
{
    List<Employee> AllEmployees = new List<Employee>();

    List<Cars> lcars1 = new List<Cars>();
    Cars car1 = new Cars();
    car1.CarType = Cars.CarTypes.Van;
    lcars1.Add(car1);lcars1.Add(car1);

    Cars car2 = new Cars();
    car2.CarType = Cars.CarTypes.SmallCar;
    lcars1.Add(car2);

    List<Cars> lcars2 = new List<Cars>();
    lcars2.Add(car1);lcars2.Add(car2);lcars2.Add(car2);

    AllEmployees.Add(new Employee(){ Name="emp1", Cars = lcars1});
    AllEmployees.Add(new Employee(){ Name="emp2", Cars = lcars2});
    AllEmployees.Add(new Employee(){ Name="emp3", Cars = lcars1 });
    AllEmployees.Add(new Employee(){ Name="emp4", Cars = lcars2});

    List<Employee> Temp = AllEmployees.Where(emps => emps.Cars.Any(cartype => cartype.CarType == Cars.CarTypes.Van)).ToList();

    List<Employee> EmployeesWithVans = (from item in Temp
            select new Employee{ 
                                        Name = item.Name, 
                                        Cars = (item.Cars.Where( car => car.CarType == Cars.CarTypes.Van)).ToList()
                                }).ToList();

    EmployeesWithVans.Dump();
}

:

enter image description here

+2

, ? ( , )

var EmployeesWithVans = AllEmployees
                        .Where(emps => emps.Cars.Any(cartype => cartype.CarType == Cars.CarTypes.Van)
                                       && !emps.Cars.Any(cartype => cartype.CarType == Cars.CarTypes.SmallCar))
                        .ToList();
0

The request is correct, it is added Employeeto the returned sequence if a van is assigned to it. I’m not quite sure where the question is, do you think that your request will not return anything, since not all employees have a van? If so, this will be the implementation of the statement Where:

foreach(var elem in input)
{
  if (predicate(elem))
    yield return elem;
}

The predicate will be applied to all elements of your sequence; if an element executes it, it will be returned as part of the sequence.

0
source

All Articles