1

I try to get the following sql written in Linq to Entities:

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

Here is my attempt in 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;
    }

The SheetCollection model:

 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; }
 }

My diagram looks like this:

enter image description here

I understand that it doesn't work out to equal with a navigation property. But how should this be done in a correct way?

4

1 回答 1

1

关于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
...

您的查询对于我来说有点太大了,无法整理出所有的连接,但这应该可以帮助您完成它。

于 2012-11-04T22:38:50.997 回答