LINQ中的默认为空无法正常工作,所以我需要查找问题所在

时间:2019-03-03 20:09:07

标签: entity-framework linq linq-to-sql

亲爱的

我有以下代码,它应该显示包括承包商和reportsourcesC在内的所有记录,但是此代码仅显示与这两个表匹配的记录,但是如果不匹配,则即使我将记录也不会显示使用DefaultIfEmpty();因此,可能是问题所在。

注意: InjuresConseqs表将与以下表结合 AccidentCategory(必需数据) 事件(不是必需的数据,此表也没有问题) InjuryTypes(必需数据) PartOfBodys(必需数据) 承包商(不需要数据,此表有问题) reportsourcesC(不是必需的数据,此表有问题) 就业猫(必填数据) 班次(不需要数据,此表也没有问题)

public List<ViweInjuresConseq> GetAllInjuresConseq(int ID= 0)
        {
            var data = (from FInjures in db.InjuresConseqs
                        join AccCat in db.AccidentCategory on FInjures.AccidentCategoryID equals AccCat.AccidentCategoryID
                        join Evnts in db.Events on FInjures.EventID equals Evnts.EventID into Eventresult
                        from Evnts in Eventresult.DefaultIfEmpty()
                        join InjType in db.InjuryTypes on FInjures.InjuryTypeID equals InjType.InjuryTypeID
                        join Partbody in db.PartOfBodys on FInjures.PartOfBodyID equals Partbody.PartOfBodyID
                        join contr in db.contractors on FInjures.Contractor_ID equals contr.Contractor_ID into contrresult
                        from contr in contrresult.DefaultIfEmpty()

                        join Report3 in db.reportsourcesC on FInjures.Reportsource3_ID equals Report3.Reportsource3_ID into Report3result
                        from Report3 in Report3result.DefaultIfEmpty()
                        join report2 in db.reportsourcesB on Report3.Reportsource2_ID equals report2.Reportsource2_ID into Report2result
                        from report2 in Report2result.DefaultIfEmpty()
                        join report1 in db.reportsourcesA on report2.Reportsource1_ID equals report1.Reportsource1_ID into Report1result
                        from report1 in Report1result.DefaultIfEmpty()

                        join Empcat in db.EmploymentCats on FInjures.EmploymentCatID equals Empcat.EmploymentCatID
                        join Shfts in db.Shifts on FInjures.ShiftID equals Shfts.ShiftID into Shftsresult
                        from Shfts in Shftsresult.DefaultIfEmpty()

                        select new ViweInjuresConseq
                        {
                            InjureID = FInjures.InjureID,
                            IN_ID = FInjures.IN_ID,
                            AccidentCategoryID = FInjures.AccidentCategoryID,
                            AccidentCategoryLabel = AccCat.AccidentCategoryD,
                            EventID = FInjures.EventID,
                            EventLabel = Evnts.EventD,
                            InjuryTypeID = FInjures.InjuryTypeID,
                            InjuryTypeLabel = InjType.InjuryTypeD,
                            InjuryTypeDes = FInjures.InjuryTypeDes,
                            PartOfBodyID = FInjures.PartOfBodyID,
                            PartOfBodyLabel = Partbody.PartOfBodyD,
                            PartOfBodyDes = FInjures.PartOfBodyDes,
                            authorities = FInjures.authorities,
                            EstmatedLWDC = FInjures.EstmatedLWDC,
                            ActualLWDC = FInjures.ActualLWDC,
                            Contractor_ID = FInjures.Contractor_ID,
                            ContractorLabel = contr == null ? string.Empty : contr.Contractor_Name,
                            Contractors = FInjures.Contractors,
                            Reportsource3_ID = FInjures.Reportsource3_ID,
                            Reportsource3Label = Report3 == null ? string.Empty : report1.Reportsource1 +"-"+ report2.Reportsource2 +"-"+ Report3.Reportsource3,
                            EmploymentCatID = FInjures.EmploymentCatID,
                            EmploymentCatLabel = Empcat.EmploymentCatDes,
                            Postion = FInjures.Postion,
                            ShiftID = FInjures.ShiftID,
                            ShiftLabel = Shfts.ShiftD,
                            DayOnTask = FInjures.DayOnTask,
                            ExInPosition = FInjures.ExInPosition,
                            ExOnTask = FInjures.ExOnTask,
                            EmID = FInjures.EmID,
                            FullName = FInjures.FullName,
                            Phone = FInjures.Phone,
                            Address = FInjures.Address,
                            comment = FInjures.comment
                        }).Where(a => a.IN_ID == ID).OrderBy(a => a.InjureID).ToList();

            return (data);
        }  

我在DbContext中放置了以下代码,以便能够迁移这两个表;因为,我在迁移过程中遇到了问题;可能是问题

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Reportsources.ReportsourceC>().HasMany(p => p.ActionData).WithRequired(a=>a.ReportsourcesC).HasForeignKey(a=>a.Reportsource3_ID).WillCascadeOnDelete(false);
            modelBuilder.Entity<Reportsources.ReportsourceC>().HasMany(p => p.InjuresConseq).WithRequired(a => a.ReportsourceC).HasForeignKey(a => a.Reportsource3_ID).WillCascadeOnDelete(false);
            modelBuilder.Entity<Attached.Contractor>().HasMany(a => a.InjuresConseq).WithRequired(b => b.Contractor).HasForeignKey(a => a.Contractor_ID).WillCascadeOnDelete(false);
            modelBuilder.Entity<Attached.Contractor>().HasMany(a => a.ContractorInvolve).WithRequired(b => b.Contractor).HasForeignKey(a => a.Contractor_ID).WillCascadeOnDelete(false);
        }

1 个答案:

答案 0 :(得分:0)

我发现问题出在这两个表中,下面两个表中的Collection代码在迁移过程中出现了问题;因此,我禁用了它们并迁移了我的数据库,并删除了DbContext中的modebuildir,我发现问题已解决,我不知道原因和技术问题是什么,因为不是LINQ专家

Contractor.cs

public ICollection<Consequences.Injures.InjuresConseq> InjuresConseq { get; set; }
public ICollection<Incidents.Incident> Incident { get; set; }
public ICollection<Incidents.ContractorInvolve> ContractorInvolve { get; set; }

ReportsourceC.cs

public ICollection<Incidents.Incident> Incident { get; set; }    
public ICollection<Consequences.Injures.InjuresConseq> InjuresConseq { get; set; }   
public ICollection<Attached.ActionData> ActionData { get; set; }