Linq包含列表中的值

时间:2011-07-18 15:35:23

标签: c# linq intersect

我很难解释这个,但我希望一些代码可以提供帮助:

        var softChannels = channels.ByPath("/software/").Children.Where(c => c.StateProperties.IsActive);

        var tmpGames = new List<MyCms.Content.Games.Game>();
        // Get games only from active game channels
        foreach (var channel in channels.ByPath("/gameslivecasinodirectcom/game-channels/").Children.Where(c => c.StateProperties.IsActive))
        {
            // QUESTION IS ABOUT THIS LINE
            tmpGames.AddRange(oGames.AllActive.Where(g => g.StateProperties.Channels.Contains(channel.Guid) && g.GamingProperties.Software.Contains(softChannels)));
        }

我想要做的是,如果g.GamingProperties.Software包含其中一个softChannel之一,则添加它。也许一个不同的认可会更好......任何建议?

p.s我知道这行不起作用,我把代码放在那里只是为了便于理解我需要的东西。

编辑: 我想我已经解决了它:

var softChannels = channels.ByPath("/software/").Children.Where(c => c.StateProperties.IsActive).Select(c => c.Guid);

var tmpGames = new List<MyCms.Content.Games.Game>();
// Get games only from active game channels
foreach (var channel in channels.ByPath("/gameslivecasinodirectcom/game-channels/").Children.Where(c => c.StateProperties.IsActive))
{
    tmpGames.AddRange(oGames.AllActive.Where(g => g.StateProperties.Channels.Contains(channel.Guid) && softChannels.Contains(g.GamingProperties.Software.Trim())));
}

如果有人发现错误,请告诉我。

1 个答案:

答案 0 :(得分:6)

您想要检查Any()的{​​{1}}是否包含在内:

softChannels

事实上,你甚至可以写

softChannels.Any(sc => g.GamingProperties.Software.Contains(sc))