I've been applying for a while DTOs in the Repository (MVC 5 + EF 6.1) because they are the only way for me to extract partial data (selecting certain columns) from the Database. To my surprise, from some recent research, this is bad
The only solution I could think of, is using IQueryable (and let's not start with this) (Plus, it's very probable that I'll get the context disposed before mapping it to a DTO) :
The question would be, how can I retrieve partial data from the database without using IQueryable?
This is how I've been doing it without problems:
public async Task<List<ParticipantIdsOnly>> GetRegisteredParticipantsIdsOnlyAsync(int tournamentId)
{
return await _dbRepositories.TournamentRepository.Where(x => x.TournamentId == tournamentId)
.SelectMany(y => y.Participants.Where(x => x.Status == ParticipantStatus.Active)
.Select(x => new ParticipantIdsOnly { Id = x.Id, ProviderPlayerId = x.ProviderPlayerId }))
.ToListAsync();
}
BTW, if I try "newing" the Entity using a select, I get a LINQ to Entities error.