linq将array和sum作为返回数据集的一部分

时间:2016-06-02 08:54:17

标签: c# linq linq-to-sql linq-to-entities linq-to-objects

我有这个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
                    };

对于最后两个结果,位置ID和鸟ID我想获得位置ID和鸟ID总和的数组的数组。

如何在linq中实现这一目标? 感谢

1 个答案:

答案 0 :(得分:1)

我不清楚你在问什么,但如果我理解你想要什么,你可以使用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)
             };