从查询中排除实体数据

时间:2012-06-04 20:07:57

标签: asp.net-mvc-3 entity-framework-4 linq-to-entities

我正在查询我的数据库,并且我有一些导航属性,我不一定要在查询中返回。我包含了很多导航属性来禁用延迟加载,但是我的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 个答案:

答案 0 :(得分:1)

好吧,如果你这样做

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

它会“急切地”加载你的葡萄酒生产商,但不会是你葡萄酒的生产者。葡萄酒......

另一种方法是使用匿名对象只获取所需的属性

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

修改

如果你还想要第一个解决方案,请看这里。避免“循环引用”。 Entity framework serialize POCO to JSON

或其他解决方案 EF 4.1 - Code First - JSON Circular Reference Serialization Error