16

我正在寻找与 Sybase 的 LIST() 或 MySQL 的 group_concat() 等效的 LINQ

它将转换:

User  Hobby
--------------
Bob   Football 
Bob   Golf 
Bob   Tennis 
Sue   Sleeping 
Sue   Drinking

到:

User  Hobby
--------------
Bob   Football, Golf, Tennis 
Sue   Sleeping, Drinking
4

6 回答 6

17

这就是 GroupBy 运算符。你在使用 LINQ to Objects 吗?

这是一个例子:

using System;
using System.Collections.Generic;
using System.Linq;

public class Test
{
    static void Main()
    {
        var users = new[]
        {
            new { User="Bob", Hobby="Football" },
            new { User="Bob", Hobby="Golf" },
            new { User="Bob", Hobby="Tennis" },
            new { User="Sue", Hobby="Sleeping" },
            new { User="Sue", Hobby="Drinking" },
        };

        var groupedUsers = users.GroupBy(user => user.User);

        foreach (var group in groupedUsers)
        {
            Console.WriteLine("{0}: ", group.Key);
            foreach (var entry in group)
            {
                Console.WriteLine("  {0}", entry.Hobby);
            }
        }
    }
}

分组就是这样——你能自己管理其余的吗?

于 2009-03-05T12:30:57.943 回答
5

看看这个解决方案是否对您有帮助:

List<User> users = new List<User>() 
{ 
    new User {Name = "Bob", Hobby = "Football" },
    new User {Name = "Bob", Hobby = "Golf"},
    new User {Name = "Bob", Hobby = "Tennis"},
    new User {Name = "Sue", Hobby = "Sleeping"},
    new User {Name = "Sue", Hobby = "Drinking"}
};

var groupedUsers = from u in users
         group u by u.Name into g
         select new
         {
             Name = g.First<User>().Name,
             Hobby = g.Select(u => u.Hobby)
         };


foreach (var user in groupedUsers)
{
    Console.WriteLine("Name: {0}", user.Name);
    foreach (var hobby in user.Hobby)
    {
        Console.WriteLine("Hobby: {0}", hobby);
    }
}
于 2009-03-05T12:40:01.620 回答
5

重新_concat您问题的方面,使用:

static class EnumerableExtensions 
{  
    public static String AsJoined( this IEnumerable<String> enumerable )
    {
        return AsJoined( enumerable, "," );
    }

    public static String AsJoined( this IEnumerable<String> enumerable, String separator )
    {
        return String.Join( separator, enumerable.ToArray() );
    }
}

bruno conde 和 Jon Skeet 的答案中的输出 foreach 可以变成:

Console.WriteLine( "User:\tHobbies");
foreach ( var group in groupedUsers )
    Console.WriteLine( "{0}:\t{1}", group.Key, group.Select( g => g.Hobby ).AsJoined( ", " ) );

...您将获得您要求的精确结果输出格式(是的,我知道其他人已经解决了您的问题,但很难抗拒!)

于 2009-03-05T12:55:20.380 回答
3

否则我们可以执行以下操作-

var users = new[]
                {
                new { User="Bob", Hobby="Football" },
                new { User="Bob", Hobby="Golf" },
                new { User="Bob", Hobby="Tennis" },
                new { User="Sue", Hobby="Sleeping" },
                new { User="Sue", Hobby="Drinking" },
                };

                var userList = users.ToList();
                var ug = (from user in users
                          group user by user.User into groupedUserList
                          select new { user = groupedUserList.Key, hobby = groupedUserList.Select(g =>g.Hobby)});

                var ug2 = (from groupeduser in ug
                          select new{ groupeduser.user, hobby =string.Join(",", groupeduser.hobby)});
于 2014-11-11T15:21:55.600 回答
2

在一个 Linq 语句中完成。我没有办法推荐代码,但它表明它可以完成。

            var groupedUsers = from user in users
                           group user by user.User into userGroup
                           select new
                           {
                               User = userGroup.Key,
                               userHobies =
                                   userGroup.Aggregate((a, b) => 
                                       new { User = a.User, Hobby = (a.Hobby + ", " + b.Hobby) }).Hobby
                           }
                            ;
        foreach (var x in groupedUsers)
        {
            Debug.WriteLine(String.Format("{0} {1}", x.User, x.userHobies));
        }
于 2009-03-08T22:17:40.147 回答
1

所有的答案都不够好;

因为这是一个数据库查询,但我们所有人都只是在内存中这样做;

diff是内存中的某些操作会发生错误can't trans to store expression;

var list = db.Users.GroupBy(s=>s.User).
             select(g=>new{user=g.Key,hobbys=g.select(s=>s.Hobby)}); // you can just do that from db

var result=list.ToList(); // this is important,to query data to memory;

var result2 = result.select(g=>new{user=g.Key,hobbyes=string.join(",",g.hobbyes)}; //then,do what you love in memory
于 2017-06-21T21:56:41.673 回答