我想我已经把自己弄糊涂了。我的数据库看起来像:
Locations [root]
Inspections [level1]
InspectionItems [level2]
InspectionReadings [level3]
Areas [level1]
Inspections [level2]
InspectionItems [level3]
InspectionReadings [level4]
每个表都通过 Guid 链接到前一个表,并且 Inspections 表具有可为空的 AreaID,因此它可以是 Locations 的直接子级。
我需要的是
for each Location
{take all InspectionReadings entities
where location == location
sort date descending
return the top Entity details}
add the Location details and the InspectionReading details into a new table
return new table
结果应该是一个带有位置列表及其最新检查阅读日期的数据网格。每个位置应该只出现一次。
我所管理的是(这在我的 DomainService.cs 中)
public IQueryable<LocationStatusList> GetLocationStatus()
{
var status = (from a in ObjectContext.Locations
from b in ObjectContext.InspectionReadings
orderby b.DateTaken descending, a.Name
select new LocationStatusList()
{
ID = b.ID,
LocationName = a.Name,
LastInspectionDate = b.DateTaken ?? DateTime.MinValue, // the data is nullable so it needs a value to return in that case
StatusNumber = b.Status ?? -1 // the data is nullable so it needs a value to return in that case
});
return status;
}
它返回整个 InspectionItems 实体及其相关位置,尽管我已经尝试过,但我找不到一种方法来做我需要的事情。
我想在 DomainService 类中为此执行所有代码,但目前我能想到的唯一方法是将查询每个位置名称作为来自视图模型的参数,返回单个实体并创建一个新列表并添加每个单独的实体。
当然,这一切都可以在 LINQ 查询中完成吗?