引发了类型为'System.StackOverflowException'的异常。 .Net Core Api

时间:2019-12-19 13:05:13

标签: c# entity-framework linq .net-core entity-framework-core

我正在使用此查询从数据库中获取数据,其工作正常

var data = _context.TrainerTrainingMapping.Where(x =>
                x.IsActive == true &&
                user.DepartmentId == 4 ? (x.Training.DepartmentId == 4 || x.Training.DepartmentId == 10) : x.Training.CreatedBy == paging.userId
                && x.Training.IsActive == true
                && x.Profile.IsActive == true
                && (user != null && user.ShowDistrictwiseData ? (user.UserDistricts.Any(z => z.DistrictId == x.Training.DistrictId)) : true)
                && (user.DomainId != null && user.DomainId != 0 ? (x.Training.User_CreatedBy.DomainId == user.DomainId) : true)
                )
                                    .Distinct()
                                    .Select(o => new
                                    {
                                        o.ProfileId,
                                        ProfilePic = o.Profile != null ? (!string.IsNullOrEmpty(o.Profile.ProfilePic)) ? o.Profile.ProfilePic : "/ProfilePics/Participants/none.jpeg" : "/ProfilePics/Participants/none.jpeg",
                                        Gender = o.Profile != null ? o.Profile.Gender : "",
                                        jobTitleName = o.Profile != null ? o.Profile.JobTitle != null ? o.Profile.JobTitle.JobTitleName : "" : "",
                                        TrainingType = o.Training != null ? o.Training != null ? new { o.Training.TrainingCategory.TrainingCategoryName, o.Training.TrainingCategory.TrainingCategoryId } : null : null,
                                        TrainerType = o.Profile.TrainingType != null ? new { o.Profile.TrainingType.TrainingTypeName, o.Profile.TrainingType.TrainingTypeId } : null,
                                        Department = o.Training != null ? o.Training.Department != null ? new { o.Training.Department.DepartmentName, o.Training.Department.DepartmentId } : null : null,
                                        District = o.Profile != null ? o.Profile.District != null ? new { o.Profile.District.DistrictName, o.Profile.DistrictId } : null : null,
                                        PlaceOfPosting = o.Profile != null ? o.Profile.PlaceOfPosting != null ? new { o.Profile.PlaceOfPosting.PlaceOfPostingName, o.Profile.PlaceOfPostingId } : null : null,
                                        Organization = o.Profile != null ? o.Profile.Organization != null ? new { o.Profile.Organization.OrganizationName, o.Profile.OrganizationId } : null : null,
                                        Tehseel = o.Profile != null ? o.Profile.Tehseel != null ? new { o.Profile.Tehseel.TehseelName, o.Profile.TehseelId } : null : null,
                                        course = o.Profile != null ? o.Profile.Course != null ? new { o.Profile.Course.CourseName, o.Profile.Course.CourseId } : null : null,
                                        Cnic = o.Profile != null ? o.Profile.Cnic : "",
                                        FirstName = o.Profile != null ? o.Profile.FirstName : "",
                                        Bps = o.Profile != null ? o.Profile.Bps : "",
                                        SeniorityNo = o.Profile != null ? o.Profile.SeniorityNo : "",
                                        Comment = o.Profile != null ? o.Profile.Comment : "",
                                        o.Profile.CreatedDate,
                                        TrainedAsTrainerInOtherFields1 = o.Profile != null ? o.Profile.TrainedAsTrainerInOtherFields1 : "",
                                        TrainedAsTrainerInOtherFields2 = o.Profile != null ? o.Profile.TrainedAsTrainerInOtherFields2 : "",
                                        TrainedAsTrainerInOtherFields3 = o.Profile != null ? o.Profile.TrainedAsTrainerInOtherFields3 : "",
                                        Experience = o.Profile != null ? o.Profile.Experience : null,
                                        MiddleName = o.Profile != null ? o.Profile.MiddleName : "",
                                        Surname = o.Profile != null ? o.Profile.Surname : "",
                                        Phone = o.Profile != null ? o.Profile.Phone : "",
                                        Email = o.Profile != null ? o.Profile.Email : "",
                                        ProfessionalRegistrationNumber = o.Profile != null ? o.Profile.ProfessionalRegistrationNumber : null,
                                        facility = o.Profile != null ? o.Profile.PlaceOfPosting != null ? o.Profile.PlaceOfPosting.FacilityType != null ? o.Profile.PlaceOfPosting.FacilityType.FacilityTypeName : "" : "" : "",
                                        Province = _context.Province.Where(x => x.ProvinceId == Convert.ToInt32(o.Profile.Province)).FirstOrDefault().Name,
                                        ParticularExperience = o.Profile != null ? o.Profile.ParticularExperience : "",
                                        PtclLandline = o.Profile != null ? o.Profile.PtclLandline : null,
                                        Level = o.Profile != null ? o.Profile.ProfessionalLevel != null ? o.Profile.ProfessionalLevel.ProfessionalLevelName : "" : "",
                                        ServingCadre = o.Profile != null ? o.Profile.ServingCadre != null ? o.Profile.ServingCadre.ServingCadreName : "" : "",
                                        TrainingCity = o.Training != null ? o.Training.City != null ? new { o.Training.City.CityName, o.Training.CityId } : null : null,
                                        Program = o.Training != null ? o.Training.Program != null ? new { o.Training.Program.DepartmentProgramName, o.Training.DepartmentProgramId } : null : null,
                                        TypeOfTrainee = o.Training != null ? o.Training.TypeOfTrainee != null ? new { o.Training.TypeOfTrainee.TypeOfTraineeName, o.Training.TypeOfTraineeId } : null : null,
                                        NumberOfParticipants = o.Training != null ? o.Training.NumberOfParticipants : 0,
                                        venue = o.Training != null ? o.Training.Venue != null ? o.Training.Venue.VenueName : "" : "",
                                        TrainingDepartment = o.Training != null ? o.Training.Department != null ? o.Training.Department.DepartmentName : "" : "",
                                        o.Training.StartDate,
                                        o.Training.EndDate,
                                        duration = o.Training != null ? o.Training.Duration : null,
                                        Specialization = o.Profile != null ? o.Profile.Specialization : "",
                                        Username = _context.Trainer.FirstOrDefault(x => x.CNIC.Contains(o.Profile.Cnic) && x.CreatedBy == paging.userId) != null ? _context.Users.FirstOrDefault(x => x.UserId == _context.Trainer.FirstOrDefault(p => p.CNIC.Contains(o.Profile.Cnic) && p.CreatedBy == paging.userId).CreatedBy).Username : o.Profile != null ? _context.Users.FirstOrDefault(x => x.UserId == o.Profile.CreatedBy) != null ? _context.Users.FirstOrDefault(x => x.UserId == o.Profile.CreatedBy).Username : "" : "",
                                        o.TrainingId,
                                        _context.Trainer.FirstOrDefault(x => x.CNIC == o.Profile.Cnic).TrainerId,
                                        o.Training.TrainingCategoryMaster.TrainingCategoryMasterName,
                                    }).ToList();

现在我需要选择另一列,我使用了下面的代码

TrainingDistrict = o.Training != null ? o.Training.District != null ? o.Training.District.DistrictName : "" : "",

现在我的查询看起来像这样。

   var data = _context.TrainerTrainingMapping.Where(x =>
                x.IsActive == true &&
                user.DepartmentId == 4 ? (x.Training.DepartmentId == 4 || x.Training.DepartmentId == 10) : x.Training.CreatedBy == paging.userId
                && x.Training.IsActive == true
                && x.Profile.IsActive == true
                && (user != null && user.ShowDistrictwiseData ? (user.UserDistricts.Any(z => z.DistrictId == x.Training.DistrictId)) : true)
                && (user.DomainId != null && user.DomainId != 0 ? (x.Training.User_CreatedBy.DomainId == user.DomainId) : true)
                )
                                    .Distinct()
                                    .Select(o => new
                                    {
                                        o.ProfileId,
                                        ProfilePic = o.Profile != null ? (!string.IsNullOrEmpty(o.Profile.ProfilePic)) ? o.Profile.ProfilePic : "/ProfilePics/Participants/none.jpeg" : "/ProfilePics/Participants/none.jpeg",
                                        Gender = o.Profile != null ? o.Profile.Gender : "",
                                        jobTitleName = o.Profile != null ? o.Profile.JobTitle != null ? o.Profile.JobTitle.JobTitleName : "" : "",
                                        TrainingType = o.Training != null ? o.Training != null ? new { o.Training.TrainingCategory.TrainingCategoryName, o.Training.TrainingCategory.TrainingCategoryId } : null : null,
                                        TrainerType = o.Profile.TrainingType != null ? new { o.Profile.TrainingType.TrainingTypeName, o.Profile.TrainingType.TrainingTypeId } : null,
                                        Department = o.Training != null ? o.Training.Department != null ? new { o.Training.Department.DepartmentName, o.Training.Department.DepartmentId } : null : null,
                                        District = o.Profile != null ? o.Profile.District != null ? new { o.Profile.District.DistrictName, o.Profile.DistrictId } : null : null,
                                        PlaceOfPosting = o.Profile != null ? o.Profile.PlaceOfPosting != null ? new { o.Profile.PlaceOfPosting.PlaceOfPostingName, o.Profile.PlaceOfPostingId } : null : null,
                                        Organization = o.Profile != null ? o.Profile.Organization != null ? new { o.Profile.Organization.OrganizationName, o.Profile.OrganizationId } : null : null,
                                        Tehseel = o.Profile != null ? o.Profile.Tehseel != null ? new { o.Profile.Tehseel.TehseelName, o.Profile.TehseelId } : null : null,
                                        course = o.Profile != null ? o.Profile.Course != null ? new { o.Profile.Course.CourseName, o.Profile.Course.CourseId } : null : null,
                                        Cnic = o.Profile != null ? o.Profile.Cnic : "",
                                        FirstName = o.Profile != null ? o.Profile.FirstName : "",
                                        Bps = o.Profile != null ? o.Profile.Bps : "",
                                        SeniorityNo = o.Profile != null ? o.Profile.SeniorityNo : "",
                                        Comment = o.Profile != null ? o.Profile.Comment : "",
                                        o.Profile.CreatedDate,
                                        TrainedAsTrainerInOtherFields1 = o.Profile != null ? o.Profile.TrainedAsTrainerInOtherFields1 : "",
                                        TrainedAsTrainerInOtherFields2 = o.Profile != null ? o.Profile.TrainedAsTrainerInOtherFields2 : "",
                                        TrainedAsTrainerInOtherFields3 = o.Profile != null ? o.Profile.TrainedAsTrainerInOtherFields3 : "",
                                        Experience = o.Profile != null ? o.Profile.Experience : null,
                                        MiddleName = o.Profile != null ? o.Profile.MiddleName : "",
                                        Surname = o.Profile != null ? o.Profile.Surname : "",
                                        Phone = o.Profile != null ? o.Profile.Phone : "",
                                        Email = o.Profile != null ? o.Profile.Email : "",
                                        ProfessionalRegistrationNumber = o.Profile != null ? o.Profile.ProfessionalRegistrationNumber : null,
                                        facility = o.Profile != null ? o.Profile.PlaceOfPosting != null ? o.Profile.PlaceOfPosting.FacilityType != null ? o.Profile.PlaceOfPosting.FacilityType.FacilityTypeName : "" : "" : "",
                                        Province = _context.Province.Where(x => x.ProvinceId == Convert.ToInt32(o.Profile.Province)).FirstOrDefault().Name,
                                        ParticularExperience = o.Profile != null ? o.Profile.ParticularExperience : "",
                                        PtclLandline = o.Profile != null ? o.Profile.PtclLandline : null,
                                        Level = o.Profile != null ? o.Profile.ProfessionalLevel != null ? o.Profile.ProfessionalLevel.ProfessionalLevelName : "" : "",
                                        ServingCadre = o.Profile != null ? o.Profile.ServingCadre != null ? o.Profile.ServingCadre.ServingCadreName : "" : "",
                                        TrainingCity = o.Training != null ? o.Training.City != null ? new { o.Training.City.CityName, o.Training.CityId } : null : null,
                                        Program = o.Training != null ? o.Training.Program != null ? new { o.Training.Program.DepartmentProgramName, o.Training.DepartmentProgramId } : null : null,
                                        TypeOfTrainee = o.Training != null ? o.Training.TypeOfTrainee != null ? new { o.Training.TypeOfTrainee.TypeOfTraineeName, o.Training.TypeOfTraineeId } : null : null,
                                        NumberOfParticipants = o.Training != null ? o.Training.NumberOfParticipants : 0,
                                        venue = o.Training != null ? o.Training.Venue != null ? o.Training.Venue.VenueName : "" : "",
                                        TrainingDepartment = o.Training != null ? o.Training.Department != null ? o.Training.Department.DepartmentName : "" : "",
                                        o.Training.StartDate,
                                        o.Training.EndDate,
                                        duration = o.Training != null ? o.Training.Duration : null,
                                        Specialization = o.Profile != null ? o.Profile.Specialization : "",
                                        Username = _context.Trainer.FirstOrDefault(x => x.CNIC.Contains(o.Profile.Cnic) && x.CreatedBy == paging.userId) != null ? _context.Users.FirstOrDefault(x => x.UserId == _context.Trainer.FirstOrDefault(p => p.CNIC.Contains(o.Profile.Cnic) && p.CreatedBy == paging.userId).CreatedBy).Username : o.Profile != null ? _context.Users.FirstOrDefault(x => x.UserId == o.Profile.CreatedBy) != null ? _context.Users.FirstOrDefault(x => x.UserId == o.Profile.CreatedBy).Username : "" : "",
                                        o.TrainingId,
                                        _context.Trainer.FirstOrDefault(x => x.CNIC == o.Profile.Cnic).TrainerId,
                                        o.Training.TrainingCategoryMaster.TrainingCategoryMasterName,
                                        TrainingDistrict = o.Training != null ? o.Training.District != null ? o.Training.District.DistrictName : "" : "",
                                    }).ToList();
  

培训与学区之间的关系是一对多(一个学区可以有很多培训)

添加此列后,发生错误的是错误的屏幕截图

enter image description here

我在下面的链接中找到了解决方案,但不知道新查询的外观如何 Solution answered here

我需要添加列的帮助。如何将此列添加到我的查询中

TrainingDistrict = o.Training != null ? o.Training.District != null ? o.Training.District.DistrictName : "" : "",

0 个答案:

没有答案