Asp.net mvc 如何将linq中的ef导航属性连接到实体
我尝试使用Linq将以下sql写入实体:Asp.net mvc 如何将linq中的ef导航属性连接到实体,asp.net-mvc,linq-to-entities,navigation-properties,Asp.net Mvc,Linq To Entities,Navigation Properties,我尝试使用Linq将以下sql写入实体: SELECT * FROM SafetySheets AS SS JOIN UserProfiles AS UP ON SS.CreatedBy = UP.UserId JOIN SafetyOfficers AS SO ON SS.SafetyOfficer_Id = SO.Id JOIN Projects AS Pr ON SS.Project_Id = Pr.Id JOIN ConstructionLocations AS CL ON SS.Con
SELECT * FROM SafetySheets AS SS
JOIN UserProfiles AS UP ON SS.CreatedBy = UP.UserId
JOIN SafetyOfficers AS SO ON SS.SafetyOfficer_Id = SO.Id
JOIN Projects AS Pr ON SS.Project_Id = Pr.Id
JOIN ConstructionLocations AS CL ON SS.ConstructionLocation_Id = CL.Id
JOIN ProductionManagers AS PM ON SS.ProductionManager_Id = PM.Id
JOIN ConstructionManagers AS CM ON SS.ConstructionManager_Id = CM.Id
以下是我在Linq的尝试:
public IQueryable<SafetySheetCollection> GetSafetySheets()
{
var query = from vSafety in _db.SafetySheets
join vUserProfile in _db.UserProfiles
on vSafety.CreatedBy equals vUserProfile.UserId
join vProject in _db.Projects
on vSafety.Id equals vProject.SafetySheets
join vConstructionLocation in _db.ConstructionLocations
on vSafety.Id equals vConstructionLocation.SafetySheets
join vSafetyOfficer in _db.SafetyOfficers
on vSafety.Id equals vSafetyOfficer.SafetySheets
join vProductionManager in _db.ProductionManagers
on vSafety.Id equals vProductionManager.SafetySheets
join vConstructionManager in _db.ConstructionManagers
on vSafety.Id equals vConstructionManager.SafetySheets
orderby vSafety.Created descending
select new SafetySheetCollection
{
ListAllSafetySheets = vSafety,
ListAllUserProfiles = vUserProfile,
ListAllProjects = vProject,
ListAllConstructionLocations = vConstructionLocation,
ListAllSafetyOfficers = vSafetyOfficer,
ListAllProductionManagers = vProductionManager,
ListAllConstructionManagers = vConstructionManager
};
return query;
}
我的图表如下所示:
我知道它不等于导航属性。但是如何正确地完成这项工作呢?上的文档并不难找到。equals
应该是标量属性,通常是与数据库中的FK约束相对应的属性
from vSafety in _db.SafetySheets
join vUserProfile in _db.UserProfiles
on vSafety.CreatedById equals vUserProfile.UserId // CreatedById !
...
我可以在评论中提到这一点,如果没有另一种更为体贴的加入方式的话,我也会这样说(有记录,但往往被忽略):
您的查询有点太大,我无法整理所有连接,但这应该可以帮助您完成。上的文档并不难找到。equals
应该是标量属性,通常是与数据库中的FK约束相对应的属性
from vSafety in _db.SafetySheets
join vUserProfile in _db.UserProfiles
on vSafety.CreatedById equals vUserProfile.UserId // CreatedById !
...
我可以在评论中提到这一点,如果没有另一种更为体贴的加入方式的话,我也会这样说(有记录,但往往被忽略):
您的查询太大了,我无法整理所有连接,但这将帮助您完成。谢谢您的帮助。我之前已经在linq到sql上进行了连接,结果正如您上面所描述的那样。但当我尝试使用linq to实体时,它不起作用。我没有掌握外键,它是ICollection的导航属性。SafetySheet表包含所有外键,使用SQL实现简单连接只是小菜一碟。但是在LINQtoEntities中有点麻烦。而且SafetySheet表与所有其他表都有多对一关系。谢谢您的帮助。我之前已经在linq到sql上进行了连接,结果正如您上面所描述的那样。但当我尝试使用linq to实体时,它不起作用。我没有掌握外键,它是ICollection的导航属性。SafetySheet表包含所有外键,使用SQL实现简单连接只是小菜一碟。但在LINQtoEntities中有点麻烦,而且SafetySheet表与所有其他表都有多对一关系。
from vUserProfile in _db.UserProfiles
from vSafety in vUserProfile.SafetySheets // not db.SafetySheets
...