首页 > 解决方案 > 执行 linq 查询时,它会抛出转换错误 system.int32

问题描述

var Appointment = (from app in _context.Appointments
                                   join pt in _context.Patients on app.PatientID equals pt.ID into 
                                   patients
                                   from pt in patients.DefaultIfEmpty()
                                   join doc in _context.Doctors on app.DoctorID equals doc.ID into 
                                   doctors
                                   from doc in doctors.DefaultIfEmpty()
                                       //join offloc in _context.OfficeLocations on app.DoctorID 
                                    equals offloc.DoctorID into officlocation
                                       //from offloc in officlocation.DefaultIfEmpty()
                                   join docavail in _context.DoctorsAvailabilities on 
                                   app.AvailabilityID equals docavail.ID into availability
                                   from docavail in availability.DefaultIfEmpty()
                                   where ((app.CreatedByReferral == ReferralSpecialistID || 
                                   app.CompanyID == CompanyID || app.UserGroupID == CompanyID) && 
                                   app.OtherPatientID == null && pt.Status == StatusConstant.Active 
                                   && app.Status != AppointmentRescheduledID && 
                                   app.IsCloseAppointment != true)
                                   select new AppointmentDataTableResult
                                   {

                                       appointmentID = app.ID,
                                       PatientID = pt.ID,
                                       patientname = (pt.NamePrefix ?? "") + " " + pt.FirstName + " " + pt.LastName + " " + (pt.NameSuffix ?? ""),
                                       ParentPatientID = 0,
                                       dateofbirth = pt.DateOfBirth,
                                       patientType = _context.CategoryDetails.Where(x => x.ItemID == app.PatientTypeID).Select(x => x.MasterData).FirstOrDefault(),
                                       phone = pt.Phone,
                                       Date = docavail.Date,
                                       Time = docavail.Time,
                                       statustext = _context.CategoryDetails.Where(x => x.ItemID == app.Status).Select(x => x.MasterData).FirstOrDefault(),
                                       doctorname = (doc.NamePrefix ?? "") + " " + doc.FirstName + " " + doc.LastName + " " + (doc.NameSuffix ?? ""),
                                       DoctorID = doc.ID,
                                       checkin = app.CheckIn,
                                       checkout = app.CheckOut,
                                       checkintime = app.CheckInTime,
                                       checkouttime = app.CheckOutTime,
                                       withoutslot = app.WithoutSlot,
                                       locatioid = app.LocationID ?? 0,
                                       AppointmentSource = app.AppointmentSource ?? 0,
                                       Status = app.Status ?? 0,
                                       IsPastAppointment = app.IsPastAppointment,
                                       casestatusID = app.CaseStatusID ?? 0,
                                       caseTypeID = app.CaseTypeID ?? 0,
                                       ReferralAttorneyID = app.ReferralAttorneyID ?? 0,
                                       ReferralProviderID = app.ReferralProviderID ?? 0,
                                       patientTypeID = app.PatientTypeID ?? 0,
                                       InsuranceID = app.InsuranceID ?? 0,
                                       PendingAppointmentDate = app.PreferredAppointmentDateTime,
                                       // AppointmentDate = docavail.Date.Value.Month + "/" + docavail.Date.Value.Day + "/" + docavail.Date.Value.Year + " " + docavail.Time.Value.Hours + ":" + docavail.Time.Value.Minutes,
                                       ReportCount = _context.DoctorReportStatus.Where(x => x.StatusID == StatusConstant.Active && x.AppointmentID == app.ID).Count(),
                                       RerecordUploadedYN = app.RerecordUploadedYN,
                                       IsAppointmentPending = app.IsAppointmentPending,
                                       IsPendingBillingRecoardYN = app.IsPendingBillingRecoardYN,
                                       strDate = docavail.Date.Value.Month + "/" + docavail.Date.Value.Day + "/" + docavail.Date.Value.Year,
                                       DateChekin = default(DateTime),
                                       totaldays = 0
                                   });

标签: c#.netlinqmodel-view-controller

解决方案


i also face same problem many time this problem occurs when you put Tolist method because before putting the Tolist() method the output generated by the query is of type
IQuerable
but this not the solution of your problem

the problem is that May be 1 you are assigning a Nullable integer type variable to non nullable integer type value or 2 comparing the Nullable integer value to Non Nullable value in Join or vice Versa

chances of second case is greater because when assigning ,c# compiler gives error check your types of compared variable and let me give some follow ups.


推荐阅读