c# - LINQ: equivalent for string_agg from PostgreSql -


i need concatenate multiple columns values single value (separated coma example). can string_agg(u.name, ',') users in postgresql. tried in linq query syntax failed - time list split values instead of 1 row values concatenated in 1 field.

let's assume have 3 tables:

doc                            user                 doc_user +----+--------------------+    +----+-----------+   +----+-----------+ | id | path               |    | id | name      |   | docid | userid |    +----+--------------------+    +----+-----------+   +----+-----------+     |  1 | "c:\\files\\a.txt" |    |  1 | "adam"    |   |  1    | 1      |    |  2 | "c:\\files\\b.txt" |    |  2 | "benny"   |   |  1    | 2      |     |  3 | "c:\\files\\c.txt" |    |  3 | "charlie" |   |  2    | 1      |  +----+--------------------+    +----+-----------+   |  2    | 2      |                                                     |  2    | 3      |                                                     +-------+--------+ 

at start trying simple join:

var model = (from d in db.docs                   join du in db.du on d.id equals du.docid                   join u in db.users on du.userid equals u.id                   select new datamodel() { docpath = d.path, username = u.name }).tolist(); 

as suspected got list separated rows:

c:\files\a.txt | adam  c:\files\a.txt | benny  c:\files\b.txt | adam c:\files\b.txt | benny  c:\files\b.txt | charlie 

but want obtain is:

c:\files\a.txt | adam, benny c:\files\b.txt | adam, benny, charlie 

string.join() not recognized ef , can't use in linq queries, aggregate() not working too. how can achieve this?

i've prepared example play it: .net fiddle.

the code below groups common documents path using linq , joins grouped document's usernames, using string.join. think don't need tolist() in line select new datamodel() { docpath = d.path, username = u.name }).tolist(); if going use solution because going use linq again.

var grouped = model.groupby(x => x.docpath); foreach (var igrouped in grouped){     string.join(",",igrouped.select(x => x.username));     console.writeline(igrouped.key + " | " + string.join(",",igrouped.select(x => x.username))); } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -