LINQ 中的默认 Empty 不起作用,所以我需要找出问题所在
Default Empty in LINQ not functioned so i need to find what is the problem
亲爱的
我有下面的代码,它应该显示包含contractors和reportsourcesC的所有记录,但是这段代码只显示与这两个table匹配的记录,但是如果没有匹配的记录不会显示,即使我使用了 DefaultIfEmpty();因此,这可能是什么问题。
注意:
InjuresConseqs table 将加入以下 tables
事故类别(必填数据)
事件(不需要数据,这个 table 没问题)
伤害类型(必填数据)
PartOfBodys(必填数据)
承包商(不需要数据,而且这个 table 有问题)
reportsourcesC(不需要数据,这个 table 有问题)
EmploymentCats(必填数据)
班次(不需要数据,这 table 没问题)
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 中以便能够迁移这两个 table;因为,我在迁移过程中遇到了问题;这可能是问题
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);
}
我发现问题出在这两个表中,这两个表中的以下 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; }
亲爱的
我有下面的代码,它应该显示包含contractors和reportsourcesC的所有记录,但是这段代码只显示与这两个table匹配的记录,但是如果没有匹配的记录不会显示,即使我使用了 DefaultIfEmpty();因此,这可能是什么问题。
注意: InjuresConseqs table 将加入以下 tables 事故类别(必填数据) 事件(不需要数据,这个 table 没问题) 伤害类型(必填数据) PartOfBodys(必填数据) 承包商(不需要数据,而且这个 table 有问题) reportsourcesC(不需要数据,这个 table 有问题) EmploymentCats(必填数据) 班次(不需要数据,这 table 没问题)
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 中以便能够迁移这两个 table;因为,我在迁移过程中遇到了问题;这可能是问题
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);
}
我发现问题出在这两个表中,这两个表中的以下 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; }