I have an entity framework query where items are displayed based on a variable set of user-defined criteria. The final query is supposed to include the related entity "Subjects", but only does so in an intermittent fashion. That is, in the same result set, some results will include their subjects while others will not. To make matters even more bizarre, the same record "a" will include its related subjects in one request, but not in another, even though I am using the same select code in both cases. Here's the relevant snippet of the final select:
var newQuery = query.Select(l => new
{
RecordId = l.RecordId,
RawFileId = l.RawFileId,
Subjects = l.RecordSubjects.Where(s => s.Subject.ClientId == data.ClientId && l.RecordId == s.RecordId && s.IsActive).Select(s => new { Name = s.Subject.Name, SubjectId = s.Subject.SubjectId, Description = s.Subject.Description }),
Url = (l.Url == null) ? "#" : l.Url, //make sure all results display a valid Url, even if the field is null
RegionCode = l.RegionCode ?? "",
RegionName = l.Region.Name ?? "",
Year = l.Year
}
I've eliminated a bunch of additional fields and other related entities for clarity.
UPDATE: To further clarify, I can run the same exact query and have the subjects appear for a specific record the first time, and have nothing show up the next. So it can't be anything about the data, I don't think.