1

これは、この投稿のフォローアップの質問です

私は2つのモデルを持っています:

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)

助けてくれてありがとう、

マーク

4

1 に答える 1

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

このコードは、必要な場所で使用できるように調整できます。

于 2012-08-03T22:02:09.357 に答える