I'm extracting a list of candidates that have each completed X number of forms. When initiating the report the report manager can select specific forms to report on or leave the field blank to get all forms.
Here's an example of my code:
var candidates = from candidate in BPData.Candidates
join programGroup in BPData.ProgramGroups on candidate.GroupID equals programGroup.GroupID
where candidate.GroupID == programGroupID
from u in BPData.Users.Where(u => u.UserID == candidate.UserID).DefaultIfEmpty()
let formEntries = from formReport in BPData.FormReports
where formReport.CandidateID == candidate.CandidateID
where (from form in forms
select form.FormID).Contains(formReport.FormID)
join actionFormEntry in BPData.ActionFormEntries on formReport.ReportKey equals actionFormEntry.ValidationKey
orderby formReport.FormID
select new FormEntryViewModel
{
... removed for brevity
}
select new CandidateFormEntriesViewModel
{
CandidateID = candidate.CandidateID,
CandidateName = candidate.Name,
FormEntries = (List<FormEntryViewModel>)formEntries
};
The following line of code:
where (from form in forms select form.FormID).Contains(formReport.FormID)
Works well if the report manager selected specific forms to report on. But, if they leave the field blank this won't work. It's supposed to come back with all forms, but obviously won't because of the way I've setup the condition.
My question:
Is it possible to do some form of condition on the where clause to cater for this (Note - this is just my rough thoughts):
where (forms.Count() > 0) ? **keep the same code as my query above** : true
This approach actually works. It extracts ALL forms if forms.Count() == 0 and only specific forms when forms.Count() > 0. But is this the right way to go about it?