使用 linq 从列表中选择项目,其中项目出现在其他两个列表中

发布于 2024-11-27 04:41:27 字数 1360 浏览 2 评论 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; games = new List();

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

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

是的,我知道在字段中保存逗号分隔的值并不好, 但我现在无法更改它(它已经在 40 多个网站上运行)

我想做的是选择该软件处于活动状态的所有游戏(通过将 softwarelistgame.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 software
game.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞取 2024-12-04 04:41:27

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

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

希望有所帮助。

Your object model does look a little odd, so I think it could be refactored somewhat, but here's my answer:

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

You could also do this, if I understand your model correctly:

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

Hope that helps.

稍尽春風 2024-12-04 04:41:27

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

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

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

looks like your solution could use some refactoring... but here's something rough to give you the idea.

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

please post class definitions if you'd like me to be more specific.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文