Linq 获取数组和求和作为返回数据集的一部分

Linq get array and sum as part of the returned dataset

我有这个 Linq 查询:

var result = from game in db.Games
                    join gameevent in db.Events
                        on game.GameId equals gameevent.GameId
                    join birdLife in db.EventBirdCaptures
                        on gameevent.EventId equals birdLife.EventId
                    select new
                    {
                        game.GameId,
                        game.Name,
                        gameevent.LocationId, - array of all the location id's based on the game id
                        birdLife.BirdId - sum of all the birdids based on event id
                    };

对于最后两个结果,location id 和 bird id,我想获取位置 id 的 id 数组和鸟类 id 的总和。

如何在 Linq 中实现?

我不清楚你在问什么,但如果我明白你想要什么,你可以使用 join-into(加入群组)而不是 join:

var result = from game in db.Games
             join gameevent in db.Events
             on game.GameId equals gameevent.GameId into events
             from _event in events
             join birdlife in db.EventBirdCaptures
             on _event.EventId equals birdlife.EventId into birds
             select new
             {
                 game.GameId,
                 game.Name,
                 locations = events.Select(e => e.LocationId),
                 birds = birds.Sum(b => b.BirdId)
             };