2012-06-04 78 views
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; } 
} 

回答

1

好,如果你只是做

.Include(n => n.Producer) //not n.Producer.Name 

它將「渴望負荷」你的酒的生產國,但不是你的酒的Producer.Wines ...

另一種方法是使用匿名對象僅取所需的性質

db.Wines.Where(n => n.WineID == WineID) 
.Select(w => new { 
     w.VarType.Cepage, 
     w.Origin.Description, 
     ///blabla 
     w.Producer.Name, 
     w.Producer.Address.Street, 
}).FirstOrDefault(); 

EDIT

看這裏,如果你仍然想要第一個解決方案。避免「循環引用」。 Entity framework serialize POCO to JSON

或另一種解決方案 EF 4.1 - Code First - JSON Circular Reference Serialization Error

+0

感謝您的幫助。我實際上做了'.Include(n => n.Producer)「,當json.net序列化w對象時它們仍然返回。不幸的是,我的類型具有相同的」名稱「字段,所以anon對象贏得' t工作Producer.Name,Origin.Name,VarType.Name – user576838

+0

anon可能會好,我想,但你也可以看看我的編輯... –