使用LINQ将多行连接成单行(CSV属性)

我正在寻找相当于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 

这是GroupBy运营商。 你使用LINQ到对象?

这是一个例子:

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

这样做的分组 – 你可以自己pipe理rest吗?

看看这个解决scheme是否有助于你

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

你的问题的_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( ", " ) ); 

…你会得到精确的结果输出格式你问(是的,我知道其他人已经解决了你的问题,但它很难抵挡!)

否则,我们可以做到以下几点 –

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

在一个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)); } 

所有的答案都不够好;

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

不同之处在于内存中的某些操作会发生错误,无法传输到存储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