user576838
user576838

Reputation: 875

Exclude Entity Data from Query

I'm querying my database and I have some navigation properties that I don't necessarily want to return in my query. I'm including a lot of the navigation properties to disable lazy loading, but I run into problems with my Producer entity. A producer has a one to many relationship to wine, and wine has a one to one relationship to producer. When I run the query, I want the producer information (name, address, phone, etc...), but I don't need the list of wines that is in the associated producer for this query. Is there a linq method that I can use to just include some of the producer fields? This is only an issue because I'm sending the object back via json, so I don't want all that extra data.

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; }
}

Upvotes: 0

Views: 1972

Answers (1)

Rapha&#235;l Althaus
Rapha&#235;l Althaus

Reputation: 60493

well, if you just do

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

it will "eager load" your wine's producer, but not your wine's Producer.Wines...

another way would be to use anonymous object to take only the desired properties

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

look here if you still want first solution,. to avoid "circular references". Entity framework serialize POCO to JSON

or another solution EF 4.1 - Code First - JSON Circular Reference Serialization Error

Upvotes: 1

Related Questions