使用 linq 从列表中选择项目,其中项目出现在其他两个列表中
我有两个包含 guid 的列表:
var activeSoftware = channels.ByPath("/software/").Children.Where(c => c.StateProperties.IsActive).Select(c => c.Guid);
var activeChannels = channels.ByPath("/game-channels/").Children.Where(c => c.StateProperties.IsActive).Select(c => c.Guid);
和另一个游戏列表:
List
游戏对象有两个可以使用的属性:
game.gamingproperties.software
- 其中包含软件的 guid game.stateproperties.channels
- 逗号分隔的 GUID 列表
是的,我知道在字段中保存逗号分隔的值并不好, 但我现在无法更改它(它已经在 40 多个网站上运行)
我想做的是选择该软件处于活动状态的所有游戏(通过将 softwarelist
与 game.gamingproperties.software
)并且它们出现的频道处于活动状态(通过检查 game.stateproperties.channels
是否包含任何 activechannels
guid )
最初,我是这样做的:
foreach (var channel in activeSoftware)
{
foreach (var match in oGames.AllActive.Where(g => !string.IsNullOrEmpty(g.GamingProperties.UrlDirectGame) && g.GamingProperties.Software.Contains(channel) && g.StateProperties.Channels.Split(',').Intersect(activeChannels).Any()))
{
games.Add(match);
}
}
但我确信我可以摆脱那些讨厌的 foreach 并只使用 linq。
i have two lists which contains guids:
var activeSoftware = channels.ByPath("/software/").Children.Where(c => c.StateProperties.IsActive).Select(c => c.Guid);
var activeChannels = channels.ByPath("/game-channels/").Children.Where(c => c.StateProperties.IsActive).Select(c => c.Guid);
and another list, of games:
List<MyCms.Content.Games.Game> games = new List<MyCms.Content.Games.Game>();
the game object has two properties that can use:
game.gamingproperties.software
- which contains the guid of the softwaregame.stateproperties.channels
- a list of comma seperated guids
yes, i know its not good to save comma seperated values in a field,
but i cannot change it at this point of time ( its allready working on 40+ sites )
what i want to do, is select all games where the software is active ( by comparing softwarelist
to the game.gamingproperties.software
) and that the channels they appear in is active ( by checking if game.stateproperties.channels
contains any of activechannels
guids )
originally, i have done this like so:
foreach (var channel in activeSoftware)
{
foreach (var match in oGames.AllActive.Where(g => !string.IsNullOrEmpty(g.GamingProperties.UrlDirectGame) && g.GamingProperties.Software.Contains(channel) && g.StateProperties.Channels.Split(',').Intersect(activeChannels).Any()))
{
games.Add(match);
}
}
but i am sure i can get rid of those nasty foreach and just use linq.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的对象模型确实看起来有点奇怪,所以我认为它可以进行一些重构,但这是我的答案:
如果我正确理解您的模型,您也可以这样做:
希望有所帮助。
Your object model does look a little odd, so I think it could be refactored somewhat, but here's my answer:
You could also do this, if I understand your model correctly:
Hope that helps.
看起来你的解决方案可以使用一些重构......但这里有一些粗略的东西可以给你这个想法。
如果您希望我更具体,请发布类定义。
looks like your solution could use some refactoring... but here's something rough to give you the idea.
please post class definitions if you'd like me to be more specific.