我在日常比较中遇到了一些问题我将在代码中看到(如何做没有编译错误的DateTime.Now.AddMonth(2)会很高兴知道),但是我在做什么真正感兴趣的是为什么当我遍历我的foreach,并尝试打印雇主组织名称时,我得到一个空指针异常。调试后确认雇主实体存在,但为空。我希望加入会让我获得这个......
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;
}
答案 0 :(得分:2)
通过选择job
,您实际上告诉实体框架您只对Job
类型的标量值感兴趣。
您可以像这样急切加载雇主财产:
var results = (from job in db.jobs.Include("employer")
// ...