0

我想我已经把自己弄糊涂了。我的数据库看起来像:

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 查询中完成吗?

4

3 回答 3

1

最终版本,如果我做对了:

var status = (from a in ObjectContext.Locations
              select new {Location = a, LastReading = a.Inspections.SelectMany(i=>i.InspectionReadings).OrderBy(r=>r.PostDate).FirstOrDefault()};
于 2012-05-16T15:41:47.240 回答
0

好的试试这个:

 var result = from l in Locations 
       from i in l.Inspestions 
       from ii in i.InspestionItems 
       from ir in ii.InspectionReadings 
       group ir by l into g 
       select new {Location = g.Key, LastReading = g.OrderBy(r=>r.DateTaken).LastOrDefault() };

Last 或 default 是因为默认排序顺序是升序的,并且您需要最近的(last)。

于 2012-05-17T14:25:39.657 回答
0

谢谢@Val,我现在拥有的代码是:

public IQueryable<LocationStatusList> GetLocationStatus()
{
   var status = (from a in ObjectContext.Locations
                 select new LocationStatusList
                 {
                    ID=a.ID,
                    Name=a.Name,
                    LastInspectionDate= (from b in ObjectContext.Inspections
                                         from c in ObjectContext.InspectionReadings
                                         where c.InspectionItemID==b.ID && b.LocationID==a.ID
                                         orderby c.DateTaken descending
                                         select c.DateTaken).FirstOrDefault()??DateTime.MinValue,


                    StatusNumber =  (from b in ObjectContext.Inspections
                                     from c in ObjectContext.InspectionReadings
                                     where c.InspectionItemID==b.ID && b.LocationID==a.ID
                                     orderby c.DateTaken descending
                                     select c.Status).FirstOrDefault()??-1,
                 });
   return status;
}

这正是我所需要的。谢谢!

于 2012-05-17T15:13:37.017 回答