2013-08-22 14 views
1

我有以下查詢:如何從linq查詢檢索聚合值?

from spl in SpeciesLists 
     join ar in Areas on spl.Station.Area equals ar.Id 
     join ground in Grounds on ar.Ground equals ground.Id 
     join re in Regions on ground.Region equals re.Id 
     where spl.Station.Trip.year ==2013 
     select new 
      { 
       SpciesCommonName = slp.Description, 
       Are = ar.description, 
       Ground = ground.Code, 
       NumberOfTripsInProtectedAreas = "To be calculated", 
      } 

「行」字可以包含一個或多個站。保護區字段位於Trip表中,可以是1或0.一個站有一個或多個物種。

如何計算保護區內的行程次數?

很多感謝

+0

你的選擇列表中包含「SLP .Description「,這意味着你的結果將包含物種列表而不是行程列表!如果你想聚合旅行,我相信你的結果應該是不同的。你能否澄清這一點? –

+0

正確,但這是因爲我需要將所選columsn的結果分組。我知道這在psot中被省略了。 –

+0

所以,你想要聚合值在三列? –

回答

1

您需要添加ProtectedArea == 1的條件下你的where子句。

而且在你的評論指出,該集團將是:slp.Description,ar.description和 ground.Code

下面是代碼:

from spl in SpeciesLists 
      join ar in Areas on spl.Station.Area equals ar.Id 
      join ground in Grounds on ar.Ground equals ground.Id 
      join re in Regions on ground.Region equals re.Id 
      where spl.Station.Trip.year ==2013 
      && spl.Station.Trip.ProtectedArea == 1 
      group spl by new { slp.Description, ar.description, ground.Code } into Result 
      select new 
       { 
        SpciesCommonName = Result.Key.Description, 
        Are = Result.Key.description, 
        Ground = Result.Key.Code, 
        NumberOfTripsInProtectedAreas = Result.Count() 
       } 
+0

嗨阿德爾,這看起來非常優雅。我會執行它,讓你知道如果這解決了我的問題。非常感謝你的努力。 –