如何从linq查询中检索聚合值?

时间:2013-08-22 04:48:04

标签: c# 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.一个站点有一个或多个物种。

如何计算保护区的行程次数?

非常感谢提前

1 个答案:

答案 0 :(得分:1)

您需要在where子句中添加ProtectedArea == 1的条件。

同样如您的评论所述,该小组将通过: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()
               }