0

我有两个包含 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<MyCms.Content.Games.Game> games = new List<MyCms.Content.Games.Game>();

游戏对象有两个可以使用的属性:

game.gamingproperties.software- 包含软件的 guid game.stateproperties.channels- 逗号分隔的 guid 列表

是的,我知道在字段中保存逗号分隔值不好,但我目前无法更改它(它已经在 40 多个站点上工作)

我想要做的是选择软件处于活动状态的所有游戏(通过比较softwarelistgame.gamingproperties.software并且它们出现的频道处于活动状态(通过检查是否game.stateproperties.channels包含任何activechannelsguid)

最初,我这样做是这样的:

    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。

4

2 回答 2

0

看起来你的解决方案可以使用一些重构......但这里有一些粗略的东西可以给你这个想法。

var new items = items.ForEach(channel => 
from g in oGames.AllActive
where (.Where(g => !string.IsNullOrEmpty(g.GamingProperties.UrlDirectGame) && g.GamingProperties.Software.Contains(channel) && g.StateProperties.Channels.Split(',')
     .Intersect(activeChannels).Any())))    
 games.add(items);

如果您希望我更具体,请发布类定义。

于 2011-08-01T10:36:03.210 回答
0

您的对象模型看起来确实有点奇怪,所以我认为可以对其进行一些重构,但这是我的答案:

var query =
    from channel in activeSoftware
    from match in oGames.AllActive
    where !string.IsNullOrEmpty(match.GamingProperties.UrlDirectGame)
    where match.GamingProperties.Software.Contains(channel)
    where match.StateProperties.Channels.Split(',').Intersect(activeChannels).Any()
    select match;

var games = query.ToList();

如果我正确理解您的模型,您也可以这样做:

var query =
    from match in oGames.AllActive
    where !string.IsNullOrEmpty(match.GamingProperties.UrlDirectGame)
    where match.GamingProperties.Software.Intersect(activeSoftware).Any()
    where match.StateProperties.Channels.Split(',').Intersect(activeChannels).Any()
    select match;

var games = query.ToList();

希望有帮助。

于 2011-08-01T10:50:12.017 回答