Q:

Exclude items from a model - MVC/ASP.Net

This is a follow up question to this post
I have two models:
CarType.cs

//
// Holds types of car - this has a one to many relationship with Car class
//
[Table("tType")]
public class CarType
{
    [Key]
    public int type_id { get; set; }
    public int dealer_id { get; set; }
    public string type_name { get; set; }
    public string type_desc { get; set; }
    public virtual ICollection<Car> Cars { get; set; }
}  

Car.cs

//
// Holds actual Cars
//

[Table("tCar")]
public class Car
{
    [Key]
    public int car_id { get; set; }
    public int dealer_id { get; set; }
    public int type_id { get; set; }
    public int car_order { get; set; }
    public string car_name { get; set; }
    public virtual ICollection<Rental> Rentals { get; set; }
    public virtual CarType CarType { get; set; }
}

I want to have a view model, where I can view car types, and how many of each car type I have within each Car Type class, so I've created a ViewModel:
public class SearchViewModel
{
    [Required]
    public DateTime From { get; set; }
    public int Nights { get; set; }
    public int dealer_id { get; set; }
    public IQueryable<CarType> CarTypes { get; set; }
    // CarTypes as above, has a one to many relationship with the class Car
}

Given a predeterminded list of actual cars (say preBookedCars), I want to remove the list of cars from the ViewObject so for example, if the model of CarType (including the one 2 many Cars) looked like this:
Mercedes (count = 3)
....Car 1
....Car 2
....Car 3
Ford (count = 3)
....Car 4
....Car 5
....Car 6
Citroen (count = 3)
....Car 7
....Car 8
....Car 9

Given a list of CARS, how would I EXCLUDE the list of cars from the CarType model - eg.
I have a list of: Car 1, Car 4, Car 8, Car 9 - so I would like the above model to show:
Mercedes (count = 2)
....Car 2
....Car 3
Ford (count = 2)
....Car 5
....Car 6
Citroen (count = 1)
....Car 7

I know it's something along the lines of (pseudo LINQ):
var freeCars = (db context).CarTypes
                        .Cars
                        .Except(preBookedCars)

Thank you for any help,
Mark

A:

I'm not sure if this is what you're looking for, but if you're just trying to filter against the Cars collection in the CarType object, you can use something like this:
var preBookedCars = new List<int> { 1, 5, 9 };
var myCarType = new CarType();
var filteredCars = myCarType.Cars.Where(c => !preBookedCars.Contains(c.car_id));

This code can be adapted to be used where ever you need.

