2

私には従業員がいます。従業員には単一の (親) 部門があります。1:N、部門から従業員へ。Employee には N 個の EmployeeToJobTitleMatchLink があります。
従業員は N 個の駐車場を持っています。

NHibernate と Linq を使用し、「FirstOrDefault」を使用する場合..

            EmployeeNHEntity firstOrDefaultEmp = session.Query<EmployeeNHEntity>()
                .Fetch(e => e.MyEmployeeToJobTitleMatchLinks)
                .Fetch(e => e.ParentDepartment)
                .Fetch(e => e.MyParkingAreas)
                .Where(e => e.EmployeeUUID == employeeUuid)
                .FirstOrDefault();

私はこのSQLを取得します。(最も重要な部分は「トップ 1」です。

exec sp_executesql N'select TOP (1)  employeenh0_.EmployeeUUID as Employee1_1_0_, myemployee1_.LinkSurrogateUUID as LinkSurr1_3_1_, department2_.DepartmentUUID as Departme1_0_2_, parkingare4_.ParkingAreaUUID as ParkingA1_5_3_, employeenh0_.MyVersionColumn as MyVersio2_1_0_, employeenh0_.SSN as SSN1_0_, employeenh0_.LastName as LastName1_0_, employeenh0_.FirstName as FirstName1_0_, employeenh0_.CreateDate as CreateDate1_0_, employeenh0_.HireDate as HireDate1_0_, employeenh0_.ParentDepartmentUUID as ParentDe8_1_0_, myemployee1_.PriorityRank as Priority2_3_1_, myemployee1_.JobStartedOnDate as JobStart3_3_1_, myemployee1_.TheEmployeeUUID as TheEmplo4_3_1_, myemployee1_.TheJobTitleUUID as TheJobTi5_3_1_, myemployee1_.TheEmployeeUUID as TheEmplo4_0__, myemployee1_.LinkSurrogateUUID as LinkSurr1_0__, department2_.MyVersionColumn as MyVersio2_0_2_, department2_.DepartmentName as Departme3_0_2_, department2_.CreateDate as CreateDate0_2_, parkingare4_.ParkingAreaName as ParkingA2_5_3_, parkingare4_.CreateDate as CreateDate5_3_, myparkinga3_.AbcEmployeeUUID as AbcEmplo1_1__, myparkinga3_.AbcParkingAreaUUID as AbcParki2_1__ from dbo.Employee employeenh0_ left outer join dbo.EmployeeToJobTitleMatchLink myemployee1_ on employeenh0_.EmployeeUUID=myemployee1_.TheEmployeeUUID left outer join dbo.Department department2_ on employeenh0_.ParentDepartmentUUID=department2_.DepartmentUUID left outer join EmployeeToParkingAreaLink myparkinga3_ on employeenh0_.EmployeeUUID=myparkinga3_.AbcEmployeeUUID left outer join dbo.ParkingArea parkingare4_ on myparkinga3_.AbcParkingAreaUUID=parkingare4_.ParkingAreaUUID where employeenh0_.EmployeeUUID=@p0',N'@p0 uniqueidentifier',@p0='64409648-15C5-4B67-BCE2-A265012E0926'

問題は、1 行 (トップ 1) を取得することです。私は1人の従業員を持っていますが、結果セットには多くの行が必要です。

ここで、以下を使用すると、目的の結果が得られます。(子オブジェクトが設定された 1 人の従業員)...サロゲート キー (EmployeeUUID) が一致する場合、コレクションには 1 つの項目があります。

            IList<EmployeeNHEntity> emps = session.Query<EmployeeNHEntity>()
                .Fetch(e => e.MyEmployeeToJobTitleMatchLinks)
                .Fetch(e => e.ParentDepartment)
                .Fetch(e => e.MyParkingAreas)
                .Where(e => e.EmployeeUUID == employeeUuid)
                .ToList();

            EmployeeNHEntity emp = null;
            if(null!=emps)
            {
                if (emps.Count > 0)
                {
                    emp = emps[0];
                }
            }

このSQLを生成する

exec sp_executesql N'select employeenh0_.EmployeeUUID as Employee1_1_0_, myemployee1_.LinkSurrogateUUID as LinkSurr1_3_1_, department2_.DepartmentUUID as Departme1_0_2_, parkingare4_.ParkingAreaUUID as ParkingA1_5_3_, employeenh0_.MyVersionColumn as MyVersio2_1_0_, employeenh0_.SSN as SSN1_0_, employeenh0_.LastName as LastName1_0_, employeenh0_.FirstName as FirstName1_0_, employeenh0_.CreateDate as CreateDate1_0_, employeenh0_.HireDate as HireDate1_0_, employeenh0_.ParentDepartmentUUID as ParentDe8_1_0_, myemployee1_.PriorityRank as Priority2_3_1_, myemployee1_.JobStartedOnDate as JobStart3_3_1_, myemployee1_.TheEmployeeUUID as TheEmplo4_3_1_, myemployee1_.TheJobTitleUUID as TheJobTi5_3_1_, myemployee1_.TheEmployeeUUID as TheEmplo4_0__, myemployee1_.LinkSurrogateUUID as LinkSurr1_0__, department2_.MyVersionColumn as MyVersio2_0_2_, department2_.DepartmentName as Departme3_0_2_, department2_.CreateDate as CreateDate0_2_, parkingare4_.ParkingAreaName as ParkingA2_5_3_, parkingare4_.CreateDate as CreateDate5_3_, myparkinga3_.AbcEmployeeUUID as AbcEmplo1_1__, myparkinga3_.AbcParkingAreaUUID as AbcParki2_1__ from dbo.Employee employeenh0_ left outer join dbo.EmployeeToJobTitleMatchLink myemployee1_ on employeenh0_.EmployeeUUID=myemployee1_.TheEmployeeUUID left outer join dbo.Department department2_ on employeenh0_.ParentDepartmentUUID=department2_.DepartmentUUID left outer join EmployeeToParkingAreaLink myparkinga3_ on employeenh0_.EmployeeUUID=myparkinga3_.AbcEmployeeUUID left outer join dbo.ParkingArea parkingare4_ on myparkinga3_.AbcParkingAreaUUID=parkingare4_.ParkingAreaUUID where employeenh0_.EmployeeUUID=@p0',N'@p0 uniqueidentifier',@p0='64409648-15C5-4B67-BCE2-A265012E0926'

[0] インデクサーのトリックは問題ありません。しかし、Linq コードを希望するほどきれいではありません。

上位 1 人の従業員を取得するための LINQ 構文ですが、すべての子オブジェクトを使用する提案はありますか?

4

1 に答える 1

3
EmployeeNHEntity firstOrDefaultEmp = session.Query<EmployeeNHEntity>()
    .Fetch(e => e.MyEmployeeToJobTitleMatchLinks)
    .Fetch(e => e.ParentDepartment)
    .Fetch(e => e.MyParkingAreas)
    .Where(e => e.EmployeeUUID == employeeUuid)
    .AsEnumerable()    <-- note this line
    .FirstOrDefault();
于 2013-10-29T10:10:16.493 に答える