如何将linq中的ef导航属性加入实体

时间:2012-11-03 20:10:47

标签: 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.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;
    }

SheetCollection模型:

 public class SafetySheetCollection
 {
    public SafetySheet ListAllSafetySheets { get; set; }
    public Project ListAllProjects { get; set; }
    public ConstructionLocation ListAllConstructionLocations { get; set; }
    public UserProfile ListAllUserProfiles { get; set; }
    public SafetyOfficer ListAllSafetyOfficers { get; set; }
    public ProductionManager ListAllProductionManagers { get; set; }
    public ConstructionManager ListAllConstructionManagers { get; set; }
 }

我的图表如下所示:

enter image description here

据我所知,它与导航属性不相符。但是如何以正确的方式做到这一点?

1 个答案:

答案 0 :(得分:1)

joins in linq上的文档并不难找到。 equals应该是标量属性,通常是与数据库中的FK约束对应的属性。

from vSafety in _db.SafetySheets
join vUserProfile in _db.UserProfiles
    on vSafety.CreatedById equals vUserProfile.UserId // CreatedById !
...

我可以在评论中提到这一点,如果没有其他的,更加简洁的加入方式我也想提出(记录在案,但不知何故经常被忽视):

from vUserProfile in _db.UserProfiles
from vSafety in vUserProfile.SafetySheets // not db.SafetySheets
...

你的查询对我来说有点太大了,无法解决所有的联接,但这应该可以帮助你完成它。