I have posted this question a while ago but got a partial answer to my issue, so I thought I post more explanation 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'm trying to get only All employees that have vans allocated to ignoring those 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 employees if at least one van is allocated to an Employee (.Any
) if I try (.All
) it will bring back nothing as not all employees has Van.
Any idea if this can be achieved using nested Lambda?
Thanks.
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 });
By the way, you cannot always use lambda expression in place of Anonymous class, because of its limitation of being SAM type. If you are using an anonymous class to implement an interface with two abstract methods then you cannot replace it with a lambda of Java 8.
The Performance of the lambda expression is better as it is pure compile-time activity and doesn't incur extra cost during runtime. However, the Performance of the anonymous inner class is lower as requires class loading at runtime.
Nested class: Use it if your requirements are similar to those of a local class, you want to make the type more widely available, and you don't require access to local variables or method parameters.
Anonymous inner class can be instantiated. Lambda Expression can't be instantiated. Inside Anonymous inner class, “this” always refers to current anonymous inner class object but not to outer object. Inside Lambda Expression, “this” always refers to current outer class object that is, enclosing class object.
Try this Instead:
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();
This is what i tried (In 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();
}
Output:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With