使用 linq 将 IGrouping 从一个类转换为另一个类

发布于 2024-12-15 16:27:35 字数 1008 浏览 5 评论 0 原文

我有两个类 Teams 和 PlayerTeams

public class PlayerTeams
{
    public int ID { get; set; }
    public string PlayerName { get; set; }
    public string PlayerCountry { get; set; }
    public bool IsActive { get; set; }

    public string PlayerTeam { get; set; }
}

public class Players
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
    public bool? Status { get; set; }
}

我有一个 PlayerTeams 列表,它按 PlayerTeam 分组,如下所示。

var groupedPlayers = teams
            .OrderBy(x => x.PlayerName)
            .GroupBy( x => x.PlayerTeam)
            .ToList();

它的类型为 List> 但我希望它的类型为 List> 因为我不想要每行的冗余关键信息。

我怎么可能做到这一点?我只能想到 IGrouping 上的 .ConvertAll() 之类的东西。我也做不到。

有没有有效的方法来做到这一点?

I have two classes Teams and PlayerTeams

public class PlayerTeams
{
    public int ID { get; set; }
    public string PlayerName { get; set; }
    public string PlayerCountry { get; set; }
    public bool IsActive { get; set; }

    public string PlayerTeam { get; set; }
}

public class Players
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
    public bool? Status { get; set; }
}

I have a list of PlayerTeams which is grouped by PlayerTeam like this.

var groupedPlayers = teams
            .OrderBy(x => x.PlayerName)
            .GroupBy( x => x.PlayerTeam)
            .ToList();

Its of type List<IGrouping<string, PlayerTeams>> but I want it to be of type List<IGrouping<string, Players>> as I do not want the redundant key information on every row.

How could I possibly achieve that? I could only think of something like .ConvertAll() on the IGrouping. I am not able to make it also.

Is there an efiicient way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

我的痛♀有谁懂 2024-12-22 16:27:35

如果您可以更改分组,我只需使用:

var groupedPlayers = teams
            .OrderBy(x => x.PlayerName)
            .GroupBy(x => x.PlayerTeam, Players.FromPlayerTeam)
            .ToList();

其中 Players.FromPlayerTeamPlayers 中的静态方法,它接受 PlayerTeam 并返回一个玩家

此外,我建议使用 ToLookup 而不是 GroupBy - Lookup 正是您想要的,而不必担心 ToList调用。

If you can change the grouping, I'd just use:

var groupedPlayers = teams
            .OrderBy(x => x.PlayerName)
            .GroupBy(x => x.PlayerTeam, Players.FromPlayerTeam)
            .ToList();

Where Players.FromPlayerTeam is a static method in Players which takes a PlayerTeam and returns a Players.

Additionally, I'd suggest using ToLookup instead of GroupBy - a Lookup is exactly what you want here, without bothering with the ToList call.

月下凄凉 2024-12-22 16:27:35

这不是测试集,只是一个想法。

如果您在将需要 IGrouping 类型的 linq 语句转换为字符串列表时遇到困难,那么您可能必须 >之前选择它。

var groupedPlayers = new List<string>();
groupedPlayers = teams
        .OrderBy(x => x.PlayerName)
        .GroupBy(x => x.PlayerTeam, Players.FromPlayerTeam)
        .Select(x => x.Key) // << added select
        .ToList();

This not testet, just an idea.

If you have trouble converting your linq statement, which is expecting the IGrouping type, to a string list, then you might have to select it before.

var groupedPlayers = new List<string>();
groupedPlayers = teams
        .OrderBy(x => x.PlayerName)
        .GroupBy(x => x.PlayerTeam, Players.FromPlayerTeam)
        .Select(x => x.Key) // << added select
        .ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文