Reputation: 23
I have an entity Auto (car)
public class Auto : BaseDbEntity
{
public string Kleur { get; set; }
public DateTime DatumGekocht { get; set; }
public string Nummerplaat { get; set; }
//relations
public Eigenaar HuidigeEigenaar { get; set; }
public AutoType HuidigeAutoType { get; set; }
}
And 2 entities linked to this one, Eigenaar (owner) and AutoType
public class Eigenaar : BaseDbEntity
{
public string Voornaam { get; set; }
public string Achternaam { get; set; }
public string Naam => $"{Voornaam} {Achternaam}";
}
public class AutoType : BaseDbEntity
{
public string Merk { get; set; }
public string Model { get; set; }
}
In my AutoDataService I have the following:
public List<Auto> GetAllAutosSortedByName(AutoCriteria criteria)
{
return GetFullyGraphedAutos()
.Where(x => string.IsNullOrEmpty(criteria.Name))
.OrderBy(x => x.Id).ToList();
}
private IIncludableQueryable<Auto, AutoType> GetFullyGraphedAutos()
{
return _entityContext.Autos.Include(x => x.HuidigeAutoType);
}
This way I have the AutoType entity linked to the main Auto entity. But I can't figure out how to also link the 3rd entity "Eigenaar" since IIncludableQueryable only allows for 2 parameters.
I would assume it needs to look something like this
private IIncludableQueryable<Auto, AutoType, Eigenaar> GetFullyGraphedAutos()
{
return _entityContext.Autos.Include(x => x.HuidigeAutoType).thenInclude(x => x.HuidigeEigenaar);
}
Obviously this doesn't work, is there another function I should use to accomplish this?
Upvotes: 0
Views: 110
Reputation: 32063
You should not depend explicitly on IIncludableQueryable
, just continue to return IQueryable
to follow the conventions of LinQ:
private IQueryable<Auto> GetFullyGraphedAutos()
{
return _entityContext.Autos
.Include(x => x.HuidigeAutoType)
.Include(x => x.HuidigeEigenaar);
}
I will also suggest you to use the asynchronous version of the I/O bound methods of DbContext
(the ones that actually execute the query, like ToList
, First
, etc), specially given that you are working on a web application:
public async Task<List<Auto>> GetAllAutosSortedByNameAsync(AutoCriteria criteria)
{
return await GetFullyGraphedAutos()
.Where(x => string.IsNullOrEmpty(criteria.Name))
.OrderBy(x => x.Id)
.ToListAsync();
}
Upvotes: 1