Linq - where not in comma separated list
I have an object named game which has a property called channels (which is comma separated since a game can relate to more then one channel).
I also have a channel object, 开发者_运维技巧which contains a property called isActive.
What I need, is a way to get all the games that have active channels.
I started writing this:
var oGames = games.AllActive.Where(
g => !g.StateProperties.Channels.Contains(
channels.All.Where(c => c.StateProperties.IsActive).ToArray()
);
but got blocked... anyone can help me ?
var oGames = games.AllActive
.Where(g => g.StateProperties.Channels.Split(',')
.Intersect(channels.Where(c => c.StateProperties.IsActive)
.Select(c => c.Name)).Any());
I'd strongly recommend moving away from a csv of channel names, and instead store a reference to them.
This would simplify the code quite a lot. Using a dictionary mapping strings to channels would also help.
精彩评论