c# - related entities included only intermittently in Entity Framework query -
i have entity framework query items displayed based on variable set of user-defined criteria. final query supposed include related entity "subjects", in intermittent fashion. is, in same result set, results include subjects while others not. make matters more bizarre, same record "a" include related subjects in 1 request, not in another, though using same select code in both cases. here's relevant snippet of 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 results display valid url, if field null regioncode = l.regioncode ?? "", regionname = l.region.name ?? "", year = l.year }
i've eliminated bunch of additional fields , other related entities clarity.
update: further clarify, can run same exact query , have subjects appear specific record first time, , have nothing show next. can't data, don't think.
try throwing .asenumerable().
before select
, i.e., query.asenumerable().select(
force retrieval of required data rather leaving subject lazy loading. i.e., perhaps of subject
entities may have been loaded ef , present, while others not present lazy loaded on demand, may why getting intermittent behavior. doing .asenumerable()
forces loading of results subject
entities database. of course, may incur performance overhead not acceptable use case.
update
also wondering if data.clientid
part of problem. data
not in sample posted.
update 2 @travisj implies in comments below, unlikely answer correct or correct @acullen72's situation. however, @acullen72 asking post stay now.
Comments
Post a Comment