I have some issues with date comparisons I’m working on that you will see in the code (how to do DateTime.Now.AddMonth(2) without a compile error would be nice to know), but what I’m really interested in is why when I iterate through my foreach, and attempt to print the employer organization name, I get a null pointer exception. Following debug confirms that the employer entity is present, but null. I expected that join would have given me access to this…
csoDBConDataContext db = new csoDBConDataContext();
db.ObjectTrackingEnabled = false;//see above comment
var results = (from job in db.jobs
join employer in db.employers on job.employer_id equals
employer.employer_id
where job.cache_major.Contains("business") &&
job.count_major <= 30 && job.del != true &&
job.joblocation != null &&
DateTime.Now.AddMonth(2).CompareTo(((DateTime)job.postdate)) >= 0 &&
DateTime.Now.CompareTo(((DateTime)job.expiredate)) >= 0 &&
job.status_id != 406
orderby Convert.ToDateTime(job.postdate).DayOfYear
select job
).Take(20);
foreach (var j in results) {
output += j.jobtitle + j.joblocation + j.expiredate + j.postdate +
j.employer.organizationname + Environment.NewLine + Environment.NewLine;
}
By selecting
job, you are effectively telling Entity Framework that you are only interested in the scalar values on theJobtype.You can eager load employer properties like this: