我正在查询我的数据库,并且我有一些导航属性,我不一定想在我的查询中返回。我包含了很多导航属性来禁用延迟加载,但是我的 Producer 实体遇到了问题。生产者与葡萄酒具有一对多的关系,而葡萄酒与生产者具有一对一的关系。当我运行查询时,我需要生产者信息(姓名、地址、电话等),但我不需要此查询的关联生产者中的葡萄酒列表。是否有一种 linq 方法可用于仅包含一些生产者字段?这只是一个问题,因为我通过 json 将对象发回,所以我不想要所有额外的数据。
Wine w = db.Wines.Where(n => n.WineID == WineID).Include(n => n.VarType).Include(n => n.Origin).Include(n => n.App)
.Include(n => n.Vintage).Include(n => n.Importer).Include(n => n.Reviews.Select(r => r.Publication))
.Include(n => n.Producer.Name).Include(n => n.Docs).FirstOrDefault();
public class Producer : Contact
{
[Key]
public int ProducerID { get; set; }
public string Name { get; set; }
public string Twitter { get; set; }
public virtual ICollection<Wine> Wines { get; set; }
public virtual ICollection<UserObj> UserObjs { get; set; }
}
public class Wine :Updater
{
public int WineID { get; set; }
//public int WineTypeID { get; set; }
[Display(Name = "Varietal/Type")]
public int? VarTypeID { get; set; }
[Display(Name = "Origin")]
public int? OriginID { get; set; }
[Display(Name = "Appellation")]
public int? AppID { get; set; }
[Display(Name = "Vintage")]
public int? VintageID { get; set; }
[Display(Name = "Importer")]
public int? ImporterID { get; set; }
public int ProducerID { get; set; }
public string Designate { get; set; }
[Display(Name = "Drink Window")]
public string DrinkWindow { get; set; }
public string Body { get; set; }
public string SKU { get; set; }
[Display(Name = "Case Production")]
public int? CaseProduction { get; set; }
[Display(Name = "Alcohol Content")]
public double? AlcoholContent { get; set; }
public string Winemaker { get; set; }
[Display(Name = "Consulting Winemaker")]
public string ConsultWinemaker { get; set; }
public bool Sustainable { get; set; }
public bool Kosher { get; set; }
public bool Organic { get; set; }
public bool Biodynamic { get; set; }
public bool SalmonSafe { get; set; }
public Boolean Active { get; set; }
public virtual WineType WineType { get; set; }
public virtual VarType VarType { get; set; }
public virtual Origin Origin { get; set; }
public virtual App App { get; set; }
public virtual Vintage Vintage { get; set; }
public virtual Importer Importer { get; set; }
public virtual Producer Producer { get; set; }
public virtual ICollection<POS> POSs { get; set; }
public virtual ICollection<Review> Reviews { get; set; }
public virtual ICollection<Doc> Docs { get; set; }
public IEnumerable<SelectListItem> BodyList { get; set; }
}