Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda with nested classes

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 });
like image 833
Maya Avatar asked Feb 26 '11 22:02

Maya


People also ask

Can we use lambda expression with class?

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.

Which is better lambda expression or anonymous inner class?

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.

When should you use nested classes?

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.

What is the difference between lambda expression and an inner class instance?

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.


1 Answers

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:

enter image description here

like image 139
Shekhar_Pro Avatar answered Oct 19 '22 21:10

Shekhar_Pro