在实体框架中,我可以在将属性添加到 select 方法时显式加载属性,如下所示:
var workhours = dbContext.WorkHours
.Select(w => new WorkHourLookupDto()
{
Id = w.Id,
StartTime = w.StartTime,
EndTime = w.EndTime,
RecreationInMinutes = w.RecreationInMinutes,
TotalWorkHoursInMinutes = w.TotalWorkHoursInMinutes,
Customer = new CustomerMiniDto
{
Id = w.Customer.Id,
Name = w.Customer.Name
},
Description = w.Description
})
.ToList();
var workhours2 = dbContext.WorkHours
.Select(w => _factory.CreateLookUpDto(w))
.ToList();
现在工作时间创建以下选择查询:
exec sp_executesql N'SELECT [w].[Id], [w].[StartTime], [w].[EndTime], [w].[RecreationInMinutes], [w].[CompanyId], [w].[Created], [w].[CreatedBy], [w].[CustomerId], [w].[Description], [w].[LastModified], [w].[LastModifiedBy], [w].[UserId], [t].[Id], [t].[Name]
FROM [WorkHours] AS [w]
INNER JOIN (
SELECT [c].[Id], [c].[Name]
FROM [Customers] AS [c]
WHERE [c].[CompanyId] = @__ef_filter__CompanyId_2
) AS [t] ON [w].[CustomerId] = [t].[Id]
WHERE ([w].[CompanyId] = @__ef_filter__CompanyId_0) AND ([w].[UserId] = @__ef_filter__p_1)',N'@__ef_filter__CompanyId_2 nvarchar(4000),@__ef_filter__CompanyId_0 nvarchar(4000),@__ef_filter__p_1 uniqueidentifier',@__ef_filter__CompanyId_2=N'3AD04E77-9654-4BBB-A8E8-4DE1335A7516',@__ef_filter__CompanyId_0=N'3AD04E77-9654-4BBB-A8E8-4DE1335A7516',@__ef_filter__p_1='8220F8B8-720D-42E8-9357-2256470EA206'
workhours2 创建查询:
exec sp_executesql N'SELECT [w].[Id], [w].[CompanyId], [w].[Created], [w].[CreatedBy], [w].[CustomerId], [w].[Description], [w].[EndTime], [w].[LastModified], [w].[LastModifiedBy], [w].[RecreationInMinutes], [w].[StartTime], [w].[UserId]
FROM [WorkHours] AS [w]
WHERE ([w].[CompanyId] = @__ef_filter__CompanyId_0) AND ([w].[UserId] = @__ef_filter__p_1)',N'@__ef_filter__CompanyId_0 nvarchar(4000),@__ef_filter__p_1 uniqueidentifier',@__ef_filter__CompanyId_0=N'3AD04E77-9654-4BBB-A8E8-4DE1335A7516',@__ef_filter__p_1='8220F8B8-720D-42E8-9357-2256470EA206'
工厂是这样的:
public WorkHourLookupDto CreateLookUpDto(WorkHour workHour)
{
return new()
{
Id = workHour.Id,
StartTime = workHour.StartTime,
EndTime = workHour.EndTime,
RecreationInMinutes = workHour.RecreationInMinutes,
TotalWorkHoursInMinutes = workHour.TotalWorkHoursInMinutes,
Customer = _customerFactory.CreateMiniDto(workHour.Customer),
Description = workHour.Description
};
}
public CustomerMiniDto CreateMiniDto(Customer customer)
{
if(customer != null)
return new()
{
Id = customer.Id,
Name = customer.Name
};
return new();
}
为什么将查询提取到不同的类(即方法)时会有所不同?
您需要返回一个
Expression
of 类型Func<WorkHour, WorkHourLookupDto>
,即它接受 aWorkHour
并返回 aWorkHourLookupDto
。您不需要为此使用函数,您可以将其放入静态字段或属性中。
现在您只需直接传入表达式即可。