0
我在查詢我的數據庫,並且我有一些導航屬性,我不一定要在查詢中返回。我包含很多導航屬性來禁用延遲加載,但是我碰到了我的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; }
}
感謝您的幫助。我實際上做了'.Include(n => n.Producer)「,當json.net序列化w對象時它們仍然返回。不幸的是,我的類型具有相同的」名稱「字段,所以anon對象贏得' t工作Producer.Name,Origin.Name,VarType.Name – user576838
anon可能會好,我想,但你也可以看看我的編輯... –