User-1355965324 posted
Hi
I have the following model. I am trying to add some additional select with depends on the condition Please can you help in my code
public class Event
{
public int EventId { get; set; }
public string EventName { get; set; }
public DateTime EventDate { get; set; }
[ForeignKey("VenueId")]
public int VenueId { get; set; }
public Venue Venue { get; set; }
public ICollection<Gig> Gigs { get; set; }
}
public class Gig
{
public int GigId { get; set; }
public string GigHeadline { get; set; }
public int GigLengthInMinutes { get; set; }
[ForeignKey("EventId")]
public int EventId { get; set; }
public Event Event { get; set; }
[ForeignKey("ComedianId")]
public int ComedianId { get; set; }
public Comedian Comedian { get; set; }
}
public class Venue
{
public int VenueId { get; set; }
public string VenueName { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public int Seating { get; set; }
public bool ServesAlcohol { get; set; }
}
public class Comedian
{
public int ComedianId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string ContactPhone { get; set; }
}
in dbContext
public DbSet<Event> Events { get; set; }
public DbSet<Comedian> Comedians { get; set; }
public DbSet<Gig> Gigs { get; set; }
public DbSet<Venue> Venues { get; set; }
I am trying to include statement with conditionally in the existing LinQ
public ICollection<Event> GetEvents(bool includeGigs = false)
{
var _events = _db.Events.Include(v => v.Venue);
// How can I add this condition with above _events
if (includeGigs)
{
_events = _events.Include(g => g.Gigs)
.ThenInclude(c => c.Comedian);
}
// How can I add Order by condition with above _events
_events = _events.OrderBy(e => e.EventDate);
return _events.ToList();
}