0

私はこれで混乱していると思います。私のデータベースは次のようになります。

Locations [root]
    Inspections [level1]
       InspectionItems [level2]
           InspectionReadings [level3]      
    Areas [level1]
       Inspections [level2]
           InspectionItems [level3]
               InspectionReadings [level4]

各テーブルはGuidによって前のテーブルにリンクされており、Inspectionsテーブルにはnull許容の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

結果は、場所のリストとそれらの最新の検査読み取り日を含むデータグリッドになるはずです。各場所は1回だけ表示されます。

私が管理しているのは(これは私の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)が必要なため、lastまたはdefault。

于 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 に答える