2012-08-03 52 views
1

这是this post排除某个模型项目 - MVC/ASP.Net

一个跟进的问题,我有两个型号:

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; } 
} 

我想有一个视图模型,在那里我可以查看汽车类型和多少各车种我有每个车型类别中,所以我创建了一个视图模型:

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),我想删除的predeterminded列表列表o f起的的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 

鉴于汽车列表,我将如何排除汽车的名单从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

http://msdn.microsoft.com/en-us/library/bb300779.aspx? – 2012-08-03 21:56:54

+0

我相信你需要的代码可以在这里找到:http://stackoverflow.com/questions/11092930/check-if-listt-contains-any-of-another-list – bUKaneer 2012-08-03 21:59:12

回答

2

我不知道这是什么你要找的,但如果你只是想筛选对在CarType对象的收集车,你可以使用这样的事情:

var preBookedCars = new List<int> { 1, 5, 9 }; 
var myCarType = new CarType(); 
var filteredCars = myCarType.Cars.Where(c => !preBookedCars.Contains(c.car_id)); 

该代码可适于用在任何你需要的。