C# 这个LINQ查询不正确吗?
所以我现在做的是一个查询,在本例中,通过员工2015年某一年的ID获取员工的缺勤情况 这是DB模型,这些是类:C# 这个LINQ查询不正确吗?,c#,linq,C#,Linq,所以我现在做的是一个查询,在本例中,通过员工2015年某一年的ID获取员工的缺勤情况 这是DB模型,这些是类: [Serializable] public class Absence { public Int32 IDAbsence; public Int32 IDEmployee; public Int32 Period; public DateTime BeginDate; public DateTime? EndDate; public A
[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;
}
}
这就是错误:
对象引用未设置为对象的实例
奇怪的是,因为我在DB中查到了该员工2015年的缺勤记录,以及相应的时间段。没有任何情况下Period未找到相应的IDTimePeriod
我错过了什么 试试看:
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;
}
如果此查询正确,您将获得预期的输出是否确实初始化了dataContext?Employee.IdeEmployee不应该只是IdeEmployee吗?请尝试将wherebStps=>abstps.Item1.Employee.IdeEmployee==employeeID&&abstps.Item2.StartTime.Year==Year替换为wherebStps=>abstps.Item1!=null&&absps.Item1.Employee!=null&&absps.Item1.Employee.ideemployee==employeeID&&absps.Item2!=null&&absps.Item2.StartTime.Year==yearYes是的,我初始化了dataContext。。。Artem说EmployeeID不应该在那里,我删除了它,因为Employee类有一个缺勤列表,所以你必须在获取员工ID之前获取该员工。Ako我仍然得到错误…LINQ to Entities中只支持无参数构造函数和初始值设定项。我成功地获取了它。。。我必须在to列表和select and expect之间切换,以获得一个IEnumerable
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;
}