我有两种模式:
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; }
}
我希望有一个视图模型,我可以在其中查看汽车类型,以及每个汽车类型类中每种汽车类型的数量,因此我创建了一个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
}
鉴于预先确定的实际汽车列表(比如preBookedCars),我想从ViewObject中删除汽车列表,例如,如果CarType模型(包括一个2辆汽车)看起来像这样:
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
鉴于CARS列表,我如何从CarType模型中排除汽车列表 - 例如。
我有一个列表:汽车1,汽车4,汽车8,汽车9 - 所以我希望上面的模型显示:
Mercedes (count = 2)
....Car 2
....Car 3
Ford (count = 2)
....Car 5
....Car 6
Citroen (count = 1)
....Car 7
我知道这是(伪LINQ):
var freeCars = (db context).CarTypes
.Cars
.Except(preBookedCars)
感谢您的帮助,
标记
答案 0 :(得分:2)
我不确定这是否是您正在寻找的,但如果您只想尝试过滤CarType对象中的Cars集合,则可以使用以下内容:
var preBookedCars = new List<int> { 1, 5, 9 };
var myCarType = new CarType();
var filteredCars = myCarType.Cars.Where(c => !preBookedCars.Contains(c.car_id));
此代码可以根据您的需要进行调整。