简体   繁体   English

在 Linq 查询上需要一些帮助,第三级包括 where

[英]Need some help on a Linq query, 3rd level include where

I've scoured the internet but not been able to find something for my specific case.我已经搜索了互联网,但无法为我的具体案例找到一些东西。

Here's my models:这是我的模型:

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

}

I need to find:我需要找到:

  • All units with statusid == 1所有 statusid == 1 的单位
  • For all those units, all reservations with reservationid == 1对于所有这些单位,所有具有 reservationid == 1 的预订

I can get the units and I can select all the reservationunits... but I can't filter the reservations of get the item of reservation included.我可以得到单位,我可以 select 所有的预订单位......但我无法过滤预订以获取包含的预订项目。 Could anyone point me in the right direction?谁能指出我正确的方向? This is what I've tried:这是我尝试过的:

var unitQuery = 
                db
                .Units
                .Where(x => x.UnitStatusID == 1 || x.UnitStatusID == 4)
                .Include(o => o.ReservationUnits.Where(p => p.Reservation.ReservationStatusID == 1))
                .ToList();

which is giving me the following error:这给了我以下错误:

System.ArgumentException: 'The Include path expression must refer to a navigation property defined on the type. System.ArgumentException: '包含路径表达式必须引用在类型上定义的导航属性。 Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.对参考导航属性使用虚线路径,对集合导航属性使用 Select 运算符。 (Parameter 'path')' (参数'路径')'

It seems that this worked for me.看来这对我有用。

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

Does this look correct?这看起来正确吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM