C# 如何使用左外联接在linq中联接四个表
我想使用LINQ连接多个表。我能够联接一个表并在视图中显示结果,但是当我试图显示其余的表时,我得到了以下错误 System.InvalidOperationException:“LINQ表达式”DbSet() .GroupJoin( 内部:DbSet(), outerKeySelector:MachineModel=>MachineModel.EnvironmentTypeID, innerKeySelector:EnvironmentTypeModel=>EnvironmentTypeModel.EnvironmentTypeID, 结果选择器:(MachineModel,表1)=>新建{ MachineModel=MachineModel, 表1=表1 })“无法翻译。以可以翻译的形式重写查询,或者通过插入对“AsEnumerable”、“asAsAsAsyncEnumerable”、“ToList”或“ToListSync”的调用显式切换到客户端计算C# 如何使用左外联接在linq中联接四个表,c#,C#,我想使用LINQ连接多个表。我能够联接一个表并在视图中显示结果,但是当我试图显示其余的表时,我得到了以下错误 System.InvalidOperationException:“LINQ表达式”DbSet() .GroupJoin( 内部:DbSet(), outerKeySelector:MachineModel=>MachineModel.EnvironmentTypeID, innerKeySelector:EnvironmentTypeModel=>EnvironmentTypeMode
[HttpGet]
public List<JoinedMachineModel> Get()
{
List<JoinedMachineModel> result = (from MachineModel in _context.Machines
join EnvironmentTypeModel in _context.EnvironmentTypeID on MachineModel.EnvironmentTypeID equals EnvironmentTypeModel.EnvironmentTypeID into table1
join HighTrustEnvironmentTypeModel in _context.HighTrustEnvironmentTypes on MachineModel.HighTrustEnvironmentTypeID equals HighTrustEnvironmentTypeModel.HighTrustEnvironmentTypeID into table2
//join PhysicalLocationTypeModel in _context.PhysicalLocationTypes on MachineModel.PhysicalLocationTypeID equals PhysicalLocationTypeModel.PhysicalLocationTypeID into table3
//join OwnerTypeModel in _context.OwnerTypes on MachineModel.OwnerTypeID equals OwnerTypeModel.OwnerTypeID into table4
from updatedtable in table1.DefaultIfEmpty()
from updatedtable2 in table2.DefaultIfEmpty()
//from updatedtable3 in table3.DefaultIfEmpty()
//from updatedtable4 in table4.DefaultIfEmpty()
orderby updatedtable2.HighTrustEnvironmentTypeID
select new
{
machineid = (int?)MachineModel.MachineID,
machinename = MachineModel.MachineName,
machinedescription = MachineModel.MachineDescription,
machineidentifier = MachineModel.MachineIdentifier,
environmentid = (int?)updatedtable.EnvironmentTypeID,
//ownerid = (int?)updatedtable4.OwnerTypeID,
hightrustenvironmentid = (int?)updatedtable2.HighTrustEnvironmentTypeID,
//physicallocationid = (int?)updatedtable3.PhysicalLocationTypeID,
createddatetime = MachineModel.CreatedDateTime,
updateddatetime = MachineModel.UpdatedDateTime,
updatedby = MachineModel.UpdatedBy,
isdeleted = (bool?)MachineModel.IsDeleted,
environmentname = updatedtable.EnvironmentTypeName,
//physicallocationname = updatedtable3.PhysicalLocationTypeName,
hightrustname = updatedtable2.HighTrustEnvironmentTypeName,
//ownername = updatedtable4.OwnerTypeName
}).ToList()
.Select(x => new JoinedMachineModel()
{
MachineID = x.machineid,
MachineName =x.machinename,
MachineDescription = x.machinedescription,
MachineIdentifier = x.machineidentifier,
EnvironmentTypeID = x.environmentid,
//OwnerTypeID = x.ownerid,
HighTrustEnvironmentTypeID = x.hightrustenvironmentid,
//PhysicallocationTypeID = x.physicallocationid,
CreatedDateTime = x.createddatetime,
UpdatedDateTime = x.updateddatetime,
UpdatedBy = x.updatedby,
IsDeleted = x.isdeleted,
EnvironmentTypeName = x.environmentname,
//PhysicalLocationTypeName = x.physicallocationname,
HighTrustEnvironmentTypeName = x.hightrustname,
//OwnerTypeName = x.ownername
}).ToList();
return (List<JoinedMachineModel>)result;
}
如果您需要更多信息,请告诉我。请参阅以下内容:重复问题。考虑一下现有的答案,如JJungYes所评论的,我找到了答案。我只是在努力确定应该对解决方案进行哪些更改以使连接正常工作。答案没有详细说明为使所有连接正常工作所做的具体操作。我需要表演一组吗。请记住,我对这方面还不太熟悉。谢谢
public class JoinedMachineModel
{
public int? MachineID { get; set; }
public string MachineName { get; set; }
public string MachineDescription { get; set; }
public string MachineIdentifier { get; set; }
[Key]
public int? EnvironmentTypeID { get; set; }
public int? OwnerTypeID { get; set; }
public int? HighTrustEnvironmentTypeID { get; set; }
public int? PhysicallocationTypeID { get; set; }
public DateTime CreatedDateTime { get; set; }
public DateTime UpdatedDateTime { get; set; }
public string UpdatedBy { get; set; }
public bool? IsDeleted { get; set; }
// Joined Columns
public string EnvironmentTypeName { get; set; }
public string PhysicalLocationTypeName { get; set; }
public string HighTrustEnvironmentTypeName { get; set; }
public string OwnerTypeName { get; set; }