我在互联网上搜索过,但没能找到适合我具体情况的东西。
这是我的型号:
public class Unit
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UnitID { get; set; }
public string Name { get; set; }
public int UnitStatusID { get; set; }
public List<ReservationUnit> ReservationUnits { get; set; }
}
public class ReservationUnit
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ReservationUnitID { get; set; }
[Required]
public int ReservationID { get; set; }
public Reservation Reservation { get; set; }
[Required]
public int UnitID { get; set; }
public Unit Unit { get; set; }
public decimal Amount { get; set; }
public bool AmountIsTaxInclusive { get; set; }
public bool IsFixedRate { get; set; }
}
public class Reservation
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ReservationID { get; set; }
public int ReservationStatusID { get; set; }
public List<ReservationUnit> Units { get; set; }
}
我需要找到:
- 状态为==1的所有单元
- 对于所有这些单元,所有带有reservationid==1的预订
我可以得到单位,我可以选择所有的保留单位。。。但我无法过滤预订,无法将预订项目包括在内。有人能给我指正确的方向吗?这就是我尝试过的:
var unitQuery =
db
.Units
.Where(x => x.UnitStatusID == 1 || x.UnitStatusID == 4)
.Include(o => o.ReservationUnits.Where(p => p.Reservation.ReservationStatusID == 1))
.ToList();
这给了我以下错误:
System.ArgumentException:'Include路径表达式必须引用在类型上定义的导航属性。对引用导航属性使用虚线路径,对集合导航属性使用Select操作符。(参数"path"(">
这似乎对我有用。
var unitQuery =
db
.Units
.Include("ReservationUnits.Reservation")
.Where(x => x.UnitStatusID == 1 || x.UnitStatusID == 4)
.Where(x => !x.ReservationUnits.Any() || x.ReservationUnits.Any(o => o.Reservation.ReservationStatusID == 1 || o.Reservation.ReservationStatusID == 2))
;
这个看起来正确吗?