使用 C# 将多个值分组到一个序列中

发布于 2024-09-28 12:30:45 字数 1566 浏览 1 评论 0原文

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

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

发布评论

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

评论(4

┈┾☆殇 2024-10-05 12:30:45

我的方法是使用正则表达式来解析数据,然后使用 linq 对其进行分组。您能向我们展示您到目前为止所做的尝试吗?

My approach would be to use a regex to parse the data and then linq to group it. Can you show us what you have tried so far?

徒留西风 2024-10-05 12:30:45

下面是一个您可以尝试的实现。

class UserLanguage
{
    public string UserName { get; set; }

    public string Language { get; set; }
}

static class EnumerableExtensions
{
    public static string Concatenate(this IEnumerable<string> source, string delimiter)
    {
        using (var enumerator = source.GetEnumerator())
        {
            if (enumerator.MoveNext())
            {
                StringBuilder builder = new StringBuilder(enumerator.Current);
                while (enumerator.MoveNext())
                {
                    builder.Append(delimiter).Append(enumerator.Current);
                }
                return builder.ToString();
            }
            else
            {
                return null;
            }
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        // assuming that you already have data stored in a structure similar to this
        var rawData = new List<UserLanguage>()
        {
            new UserLanguage { UserName = "Bob", Language = "English" },
            new UserLanguage { UserName = "Bob", Language = "French" },
            new UserLanguage { UserName = "Alan", Language = "Italian" },
            new UserLanguage { UserName = "Alan", Language = "Spanish" },
            new UserLanguage { UserName = "Alan", Language = "German" },
        };

        // group these objects together by UserName
        var groupedData = rawData.GroupBy(userLanguage => userLanguage.UserName);

        foreach (var grouping in groupedData)
        {
            Console.WriteLine(grouping.Key + "\t" + grouping.Select(userLanguage => userLanguage.Language).Concatenate("/"));
        }
    }
}

Below is an implementation that you can play around with.

class UserLanguage
{
    public string UserName { get; set; }

    public string Language { get; set; }
}

static class EnumerableExtensions
{
    public static string Concatenate(this IEnumerable<string> source, string delimiter)
    {
        using (var enumerator = source.GetEnumerator())
        {
            if (enumerator.MoveNext())
            {
                StringBuilder builder = new StringBuilder(enumerator.Current);
                while (enumerator.MoveNext())
                {
                    builder.Append(delimiter).Append(enumerator.Current);
                }
                return builder.ToString();
            }
            else
            {
                return null;
            }
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        // assuming that you already have data stored in a structure similar to this
        var rawData = new List<UserLanguage>()
        {
            new UserLanguage { UserName = "Bob", Language = "English" },
            new UserLanguage { UserName = "Bob", Language = "French" },
            new UserLanguage { UserName = "Alan", Language = "Italian" },
            new UserLanguage { UserName = "Alan", Language = "Spanish" },
            new UserLanguage { UserName = "Alan", Language = "German" },
        };

        // group these objects together by UserName
        var groupedData = rawData.GroupBy(userLanguage => userLanguage.UserName);

        foreach (var grouping in groupedData)
        {
            Console.WriteLine(grouping.Key + "\t" + grouping.Select(userLanguage => userLanguage.Language).Concatenate("/"));
        }
    }
}
尐偏执 2024-10-05 12:30:45

class Person
{
    string UserName {get; set;}
    string LanguageSpoken {get; set;}    
}

var table = new List<Person>() {
    new Person() {
        UserName = "Bob";
        LanguageSpoken = "English"
    }
    /* next persons*/
};

var userNameGroups = from n in table
    group n by n.UserName into g
    select new {
        keyUserName = g.Key,
        LanguageSpoken = g
    };

foreach(var g in userNameGroups) {
    Console.Write(g.keyUserName.ToString());
    foreach(var n in g.LanguageSpoken) {
        Console.WriteLine(n.ToString());
    }
}



class Person
{
    string UserName {get; set;}
    string LanguageSpoken {get; set;}    
}

var table = new List<Person>() {
    new Person() {
        UserName = "Bob";
        LanguageSpoken = "English"
    }
    /* next persons*/
};

var userNameGroups = from n in table
    group n by n.UserName into g
    select new {
        keyUserName = g.Key,
        LanguageSpoken = g
    };

foreach(var g in userNameGroups) {
    Console.Write(g.keyUserName.ToString());
    foreach(var n in g.LanguageSpoken) {
        Console.WriteLine(n.ToString());
    }
}


想念有你 2024-10-05 12:30:45

如果您的列表类似于:

class Person
{
    public string Name { get; set; }
    public string Language { get; set; }
}

List<Person> myList = /* Initialization */

那么 LINQ GroupBy 应该可以解决问题:

myList.GroupBy(a => a.Name);

希望这会有所帮助。

If your List is something like:

class Person
{
    public string Name { get; set; }
    public string Language { get; set; }
}

List<Person> myList = /* Initialization */

Then LINQ GroupBy should do the trick:

myList.GroupBy(a => a.Name);

Hope this helps.

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