1

Linq to Entities で記述された次の 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; }
 }

私の図は次のようになります。

ここに画像の説明を入力

ナビゲーション プロパティと同等に機能しないことは理解しています。しかし、これを正しい方法で行うにはどうすればよいでしょうか。

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 に答える