NHibernate Linq //带有子对象的FirstOrDefault //可能吗?

时间:2013-10-28 22:28:23

标签: nhibernate linq-to-nhibernate

我有一名员工。 员工有一个(父)部门。 1:N,部门到员工。 员工有N个EmployeeToJobTitleMatchLink(s) 员工有N个ParkingArea(s)。

使用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个Employee,但我(应该)在结果集中有很多行。

现在,如果我使用下面的内容,我会得到所需的结果。 (填充子对象的单个员工)....如果代理键(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,但是所有的子对象都是?

1 个答案:

答案 0 :(得分: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();