I am creating a web application to manage a flight school and I have the following database structure (unrelevant tables have been blurred and relevant ones have been translated to English for clarity):
I have a table Deadlines that stores all deadlines related to an aircraft. This table is linked to the Aircrafts table. Then I have a OwnedAircrafts table that stores ids of aircrafts and persons (because a person can own several aircrafts or shares of them).
My question is:
Given the id of a person, how can I retrieve the list of deadlines related to his/her aircrafts? (preferrably using method syntax)
In other words:
The following code now returns all the aircraft deadlines, no matter who the owner is. How can I reference the OwnedAircrafts table to filter only the deadlines related to personId?
IEnumerable<DeadlineViewModel> GetDeadlinesOfAPerson(long personId)
{
return _context.Deadlines
.Select(deadline => new DeadlineViewModel
{
Id = deadline.Id,
Aircraft = deadline.Aircraft,
Model = deadline.AircraftsNavigation.Model,
IdDeadlineType = deadline.DeadlineType,
DeadlineType = deadline.DeadlineTypesNavigation.Description
// other fields...
});
Thank you in advance!
CodePudding user response:
Try following :
IEnumerable<DeadlineViewModel> GetDeadlinesOfAPerson(long personId)
{
return _context.Deadlines
.Where(x => x.deadline.ID == 12345)
.Select(deadline => new DeadlineViewModel
{
Id = deadline.Id,
Aircraft = deadline.Aircraft,
Model = deadline.AircraftsNavigation.Model,
IdDeadlineType = deadline.DeadlineType,
DeadlineType = deadline.DeadlineTypesNavigation.Description
// other fields...
});
CodePudding user response:
You can add navigation properties to your entities and then try something like this:
var user = _context.Users
.Include(user => user.OwnedAircrafts)
.ThenInclude(ownedAircraft => ownedAircraft.Aircraft)
.ThenInclude(aircraft => aircraft.Deadlines)
.FirstOrDefault(user => user.Id == userId)
return user.OwnedAircrafts
.SelectMany(ownedAircraft => ownedAircraft.Aircraft.Deadlines);
CodePudding user response:
Follow this approach :
IEnumerable<DeadlineViewModel> GetDeadlinesOfAPerson(long personId)
{
return _context
.OwnedAircarfs
.Where( s => s.Persona == YourPersonId)
.Includes(s=> s.AirCrafts)
.ThenIncludes(s=> s.Deadlines)
.Select(deadline => new DeadlineViewModel
{
//use navigation properties to get value of related tables
// other fields...
}).ToList();
CodePudding user response:
You do not need to query the Persons table, as the person Id is available in OwendAircrafts
IEnumerable<DeadlineViewModel> GetDeadlinesOfAPerson(long personId)
{
return _context.Deadlines
.Where(deadline =>
deadline.Aircraft.OwnedAircrafts.Any(oa => oa.persona == personId))
.Select(deadline => new DeadlineViewModel
{
Id = deadline.Id,
Aircraft = deadline.Aircraft,
Model = deadline.AircraftsNavigation.Model,
IdDeadlineType = deadline.DeadlineType,
DeadlineType = deadline.DeadlineTypesNavigation.Description
// other fields...
});
}
This assumes that you have a navigation property OwnedAircrafts in Aircraft.

