Here's the setup I have currently
public async Task<Logic.Objects.Profile> GetProfileByEmailAsync(string email)
{
try
{
Logic.Objects.Profile logicProfile = Mapper.MapProfile(await santaContext.Client
.Include(r => r.ClientRelationXrefSenderClient)
.ThenInclude(clXref => clXref.RecipientClient)
.ThenInclude(c => c.SurveyResponse)
.ThenInclude(sr => sr.Survey)
.ThenInclude(s => s.EventType)
.Include(r => r.ClientRelationXrefSenderClient)
.ThenInclude(clXref => clXref.RecipientClient)
.ThenInclude(c => c.SurveyResponse)
.ThenInclude(sr => sr.SurveyQuestion) // This has a property named SenderCanView. I want to get only the questions where this is set to true
.Include(r => r.ClientRelationXrefSenderClient)
.ThenInclude(e => e.EventType)
.Include(s => s.ClientStatus)
.Include(c => c.SurveyResponse)
.ThenInclude(s => s.SurveyQuestion)
.Include(c => c.SurveyResponse)
.ThenInclude(sr => sr.Survey)
.ThenInclude(s => s.EventType)
.FirstOrDefaultAsync(c => c.Email == email)) ;
return logicProfile;
}
catch (Exception e)
{
throw e.InnerException;
}
}
This gets all the various things I need through my entities and such, but I want to have it only include the survey response objects where the SurveyQuestion
object has the SenderCanView
property equal to true
. How is something like this done? I wasn't able to do it with lambdas, as apparently doing .ThenInclude(sr => sr.SurveyQuestion.SenderCanView == true)
is not a valid option to use. What should I do in this case?
More information edit:
This is in essence what I want to accomplish, but in the LINQ query, rather than outside it in a double foreach
Logic.Objects.Profile logicProfile = Mapper.MapProfile(await santaContext.Client
.Include(r => r.ClientRelationXrefSenderClient)
.ThenInclude(clXref => clXref.RecipientClient)
.ThenInclude(c => c.SurveyResponse)
.ThenInclude(sr => sr.Survey)
.ThenInclude(s => s.EventType)
.Include(r => r.ClientRelationXrefSenderClient)
.ThenInclude(clXref => clXref.RecipientClient)
.ThenInclude(c => c.SurveyResponse)
.ThenInclude(sr => sr.SurveyQuestion)
.Include(r => r.ClientRelationXrefSenderClient)
.ThenInclude(e => e.EventType)
.Include(s => s.ClientStatus)
.Include(c => c.SurveyResponse)
.ThenInclude(s => s.SurveyQuestion)
.Include(c => c.SurveyResponse)
.ThenInclude(sr => sr.Survey)
.ThenInclude(s => s.EventType)
.FirstOrDefaultAsync(c => c.Email == email));
foreach(ProfileRecipient recipient in logicProfile.recipients)
{
List<Response> responsesToHide = recipient.responses.Where(r => r.surveyQuestion.senderCanView == false).ToList();
foreach(Response response in responsesToHide)
{
recipient.responses.Remove(response);
}
}