ASP.NET MVC Linq查询DefaultIfEmpty不起作用

时间:2018-02-20 17:16:14

标签: c# asp.net-mvc linq

我在Linq中有这个查询,它返回一些值:

public List<AttendanceDetail> GetCourseAttendanceForDate(int courseId, int calendarId)
        {


        using (var db = new HIQTrainingEntities())
                {


                        var attendance = from ca in db.Calendars
                                         join i in db.Inscriptions on ca.CourseId equals i.CourseId
                                         join a in db.Attendances on new { s1 = i.Id, s2 = ca.Id } equals new { s1 = a.InscriptionId, s2 = a.CalendarId } into attendanceTable
                                         from z in attendanceTable.DefaultIfEmpty()
                                         where (ca.CourseId == courseId && ca.Id == calendarId)
                                         orderby i.Person.Name
                                         select new AttendanceDetail
                                         {
                                              StudentName = i.Person.Name,
                                             CompanyName = i.Person.Company.Name,
                                             InscriptionId = i.Id,
                                             StudentId = i.PersonId,
                                             CalendarId = ca.Id,
                                             Date = ca.CalendarDate,
                                             Id = z.Id,
                                             Status = z.Status,
                                             Observation = z.Observation,
                                             UserCreated = z.UserCreated,
                                             CreatedDate = z.CreatedDate,
                                             UserUpdated = z.UserUpdated,
                                             UpdateDate = z.UpdateDate
                                         };
                        return attendance.ToList();
                    }
    }

当我执行此查询时,这是出现的错误:

  

发生了'System.InvalidOperationException'类型的异常   EntityFramework.dll但未在用户代码中处理

     

其他信息:转换为值类型'System.Int32'失败   因为物化值为空。结果类型   泛型参数或查询必须使用可空类型。

为什么我的DefaultIfEmpty不工作?

0 个答案:

没有答案