Asp.net mvc 3 从查询中排除实体数据

Asp.net mvc 3 从查询中排除实体数据,asp.net-mvc-3,entity-framework-4,linq-to-entities,Asp.net Mvc 3,Entity Framework 4,Linq To Entities,我正在查询我的数据库,我有一些导航属性,我不一定要在查询中返回这些属性。我包含了很多导航属性来禁用延迟加载,但我的生产者实体遇到了问题。生产者与葡萄酒有一对多的关系,葡萄酒与生产者有一对一的关系。当我运行查询时,我需要生产者信息(姓名、地址、电话等),但我不需要此查询关联生产者中的葡萄酒列表。是否有一种linq方法可以用于只包含一些生产者字段?这只是一个问题,因为我通过json发送对象,所以我不需要所有额外的数据 Wine w = db.Wines.Where(n => n.WineID

我正在查询我的数据库,我有一些导航属性,我不一定要在查询中返回这些属性。我包含了很多导航属性来禁用延迟加载,但我的生产者实体遇到了问题。生产者与葡萄酒有一对多的关系,葡萄酒与生产者有一对一的关系。当我运行查询时,我需要生产者信息(姓名、地址、电话等),但我不需要此查询关联生产者中的葡萄酒列表。是否有一种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; }
}
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);
公共类制作人:联系
{
[关键]
public int ProducerID{get;set;}
公共字符串名称{get;set;}
公共字符串Twitter{get;set;}
公共虚拟ICollection{get;set;}
公共虚拟ICollection UserObjs{get;set;}
}
公共类葡萄酒:更新程序
{
public int WineID{get;set;}
//public int WineTypeID{get;set;}
[显示(名称=“品种/类型”)]
公共int?VarTypeID{get;set;}
[显示(Name=“原点”)]
public int?OriginID{get;set;}
[显示(名称=“称谓”)]
公共int?AppID{get;set;}
[显示(Name=“Vintage”)]
public int?VintageID{get;set;}
[显示(Name=“进口商”)]
公共int?ImporterID{get;set;}
public int ProducerID{get;set;}
公共字符串指定{get;set;}
[显示(Name=“饮料窗口”)]
公共字符串DrinkWindow{get;set;}
公共字符串体{get;set;}
公共字符串SKU{get;set;}
[显示(Name=“案例制作”)]
公共int?CaseProduction{get;set;}
[显示(名称=“酒精含量”)]
公共双内容{get;set;}
公共字符串酿酒师{get;set;}
[显示(Name=“咨询酿酒师”)]
公共字符串生成器{get;set;}
公共布尔{get;set;}
公共卫生{get;set;}
公共布尔有机{get;set;}
公共布尔生物动力学{get;set;}
公共布尔SalmonSafe{get;set;}
公共布尔活动{get;set;}
公共虚拟WineType WineType{get;set;}
公共虚拟VarType VarType{get;set;}
公共虚拟源{get;set;}
公共虚拟应用程序App{get;set;}
公共虚拟年份{get;set;}
公共虚拟导入器导入器{get;set;}
公共虚拟生产者{get;set;}
公共虚拟ICollection POSs{get;set;}
公共虚拟ICollection审阅{get;set;}
公共虚拟ICollection文档{get;set;}
公共IEnumerable正文列表{get;set;}
}
好吧,如果你这么做的话

.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();
编辑

如果您仍然需要第一个解决方案,请查看此处,。避免“循环引用”。

还是另一种解决方案

谢谢你的帮助。实际上,我确实执行了“.Include(n=>n.Producer)”,当json.net序列化w对象时,它们仍然会返回。不幸的是,我的类型中有个具有相同的“name”字段,因此anon对象无法工作。Producer.name、Origin.name、VarType.name