0

I have this query in Linq which returns some values:

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();
                    }
    }

When I execute this query this is the error that comes:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: The cast to value type 'System.Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.

Why isn't my DefaultIfEmpty working ?

JoeSaladas
  • 69
  • 1
  • 9

0 Answers0