gpt4 book ai didi

NHibernate Linq//FirstOrDefault 与子对象//可能吗?

转载 作者:行者123 更新时间:2023-12-04 04:36:37 30 4
gpt4 key购买 nike

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

使用 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 语法来获得前 1 名员工,但使用所有子对象?

最佳答案

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();

关于NHibernate Linq//FirstOrDefault 与子对象//可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19646249/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com