简体   繁体   English

通过 LINQ 语句 C# 从 Group 返回多个值

[英]Returning multiple values from a Group by LINQ statement C#

Hi I am having trouble getting the correct values from my group by statement.嗨,我无法通过语句从我的组中获取正确的值。 I am trying to get a value grouped by another value here:我试图在此处获取按另一个值分组的值:

var allGuilds = Player.dictGuilds;


var guildTypeRoster =
    from g in allGuilds
    where g.Value.GuildType.ToString() == CBGuildType.SelectedItem.ToString()
    orderby g.Value.GuildName
    group g.Value.GuildServer.ToString() by g.Value.GuildName;

So basically I'm trying to group the names in guild by the server they are on.所以基本上我试图通过他们所在的服务器对公会中的名称进行分组。 I run into problems when trying to output both the server I am grouping by and the name here:我在尝试输出分组所依据的服务器和此处的名称时遇到问题:

foreach (var pair in guildTypeRoster)
{
    OutputBox.AppendText(pair.Key + "\n");
}

I can only get the key of the value which is the names I grouped by.我只能得到值的键,即我分组的名称。 I'm trying to also display the server and have both display but I cannot seem to find a way to do that.我正在尝试同时显示服务器并同时显示两者,但我似乎无法找到一种方法来做到这一点。 If anyone could help that would be great.如果有人可以提供帮助,那就太好了。

You will want to iterate over each group's records so you need an additional loop.您将需要遍历每个组的记录,因此您需要一个额外的循环。

foreach (var pair in guildTypeRoster)
{
    foreach(var value in pair) 
    {
        OutputBox.AppendText(pair.Key + ": " + value.GuildName + "\n");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM