简体   繁体   中英

unify multiple fields with the same ID into one field SQL Linq

I have these data, which look like this:

   ID      name
------------------
   1       Judith
   1       John
   1       Tim
   2       Anna
   2       Tyler

And I need to unify that table, having an output like this:

   ID      name
-------------------------------
   1       Judith, John, Tim
   2       Anna, Tyler

How can I do a Linq query in C# to get that result? (Or at least in SQL...)

Thank you!

in T-SQL you can use STRING_AGG

select id, STRING_AGG(name,',') 
from users GROUP BY id

Here's a sqlfiddle link http://sqlfiddle.com/#!18/e5bb27/2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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