Linq Contains value in list

Rafael Herscovici picture Rafael Herscovici · Jul 18, 2011 · Viewed 10.5k times · Source

its hard for me to explain this one, but i hope some code will help:

        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)));
        }

what i want to do is, if g.GamingProperties.Software contains one of the Guids of softChannels, then add it. maybe a diffrent approace will be better... any suggestions ?

p.s i know that line is not working, i have put the code there only for easy understanding what i need.

EDIT: i think i have resolved it:

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())));
}

if anyone sees something wrong with that, please let me know.

Answer

SLaks picture SLaks · Jul 18, 2011

You want to check whether Any() of the softChannels are contained:

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

In fact, you can even write

softChannels.Any(g.GamingProperties.Software.Contains)