c# - Linq Conditional DefaultIfEmpty query filter -
i have query below:
bool variable = false//or true var query = e in _repository.getall<entity>() u in e.users (e.auditquestiongroupid != null ? e.auditquestiongroupid : 0) == this.loggedinentity.auditquestiongroupid p in e.practitionerprofiles.defaultifempty() select new { entity = e, user = u, profile = p }; this works correctly. however, have boolean variable should determine whether join e.practitionerprofiles should have defaultifempty, thereby making left outer join instead of inner join.
however, using annoymous objects, can't figure out how correctly. want ability switch between left , inner join without duplicating whole query like:
if(variable) { var query = e in _repository.getall<entity>() u in e.users (e.auditquestiongroupid != null ? e.auditquestiongroupid : 0) == this.loggedinentity.auditquestiongroupid p in e.practitionerprofiles select new { entity = e, user = u, profile = p }; } else { var query = e in _repository.getall<entity>() u in e.users (e.auditquestiongroupid != null ? e.auditquestiongroupid : 0) == this.loggedinentity.auditquestiongroupid p in e.practitionerprofiles.defaultifempty() select new { entity = e, user = u, profile = p }; } is there clean way 1 query? problem have number of further conditions placed on it, declaring query inside loop means doesn't have local variable , don't know how create empty iqueryable anonymous object.
why not use ternary operator?
from p in (variable ? e.practitionerprofiles : e.practitionerprofiles.defaultifempty())
Comments
Post a Comment