所以我正在做的是从一个员工(通过他的身份证)获得特定年份(在这种情况下为2015年)的查询。
这是数据库模型,这些是类:
[Serializable]
public class Absence
{
public Int32 IDAbsence;
public Int32 IDEmployee;
public Int32 Period;
public DateTime BeginDate;
public DateTime? EndDate;
public AbsenceType AbsenceType;
public AbsenceStatus AbsenceStatus;
public List<Justification> Justifications;
public List<Notification> Notifications;
public Absence() { }
public Absence(Int32 IDAbsence, Int32 IDEmployee, Int32 Period, DateTime BeginDate, DateTime? EndDate, AbsenceType AbsenceType, AbsenceStatus AbsenceStatus, List<Justification> Justifications, List<Notification> Notifications)
{
this.IDAbsence = IDAbsence;
this.IDEmployee = IDEmployee;
this.Period = Period;
this.BeginDate = BeginDate;
this.EndDate = EndDate;
this.AbsenceType = AbsenceType;
this.AbsenceStatus = AbsenceStatus;
this.Justifications = Justifications;
this.Notifications = Notifications;
}
}
[Serializable]
public class TimePeriod
{
public Int32 IDTimePeriod;
public DateTime StartTime;
public DateTime EndTime;
public TimePeriod() { }
public TimePeriod(Int32 IDTimePeriod, DateTime StartTime, DateTime EndTime)
{
this.IDTimePeriod = IDTimePeriod;
this.StartTime = StartTime;
this.EndTime = EndTime;
}
}
TimePeriod
主键(IDTimePeriod
)应该是缺勤表中外键Period的引用,但我无法为Period创建外键(是的,DB I'与之合作非常令人困惑......)。
这是我的LINQ查询:
public static List<Tuple<Absence,TimePeriod>> GetAbsencesByEmployeeIDAndYear(Int32 employeeID, Int32 year) {
List<Tuple<Absence,TimePeriod>> absences = dataContext.Absences.Join(dataContext.TimePeriods,
abs => abs.Period,
tps => tps.IDTimePeriod,
(abs, tps) => new Tuple<Absence, TimePeriod>(abs, tps))
.Where(abstps => abstps.Item1.Employee.IDEmployee == employeeID && abstps.Item2.StartTime.Year == year).ToList();
return absences;
}
这就是错误:
对象引用未设置为对象的实例。
奇怪的是,因为我查阅了数据库,并且2015年该员工的缺勤条目与各自的TimePeriod相同。在任何情况下,Period都没有找到相应的IDTimePeriod。
我错过了什么?
答案 0 :(得分:0)
尝试一下:
public static List<Tuple<Absence,TimePeriod>> GetAbsencesByEmployeeIDAndYear(Int32 employeeID, Int32 year) {
List<Tuple<Absence,TimePeriod>> absences = dataContext.Absences.Join(dataContext.TimePeriods,
abs => abs.Period,
tps => tps.IDTimePeriod,
(abs, tps) => new { abs, tps})
.Where(x => x.abs.Employee.IDEmployee == employeeID && x.tps.StartTime.Year == year)
.Select(t => new Tuple<Absence,TimePeriod>(t.abs, t.tps))
.ToList();
return absences;
}