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:
I understand that it doesn't work out to equal with a navigation property. But how should this be done in a correct way?