2

我有一个员工。员工有一个(父)部门。1:N,部门到员工。员工有 N 个 EmployeeToJobTitleMatchLink(s)。
员工有 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。(最重要的部分是“Top 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 名员工,但我(应该)在结果集中有很多行。

现在,如果我使用以下内容,我会得到想要的结果。(填充了子对象的单个员工)....如果代理键 (EmployeeUUID) 上存在匹配项,则集合中有一个项目。

            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 代码那样干净。

现在有什么建议可以使用 LINQ 语法来获得 Top 1 Employee,但包含所有子对象?

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 回答