2

I'm trying to do a groupby distinct, then build a csv string:

enter image description here

[FROM] is a one-to-many join:

        var allCustomerRoles = (from cr in Customers
                                join r in CustomerRoles
                                on cr.Role_ID equals r.Role_ID
                                select new { cr.Customer_No_, r.Role_ID });

So the question is, can you please show me how to write the LINQ query to arrive at the [TO] structure, where Customer_No_ is distinct and it's Role_ID values are concatenated into a CSV string.

Joe C
  • 137
  • 1
  • 1
  • 9

1 Answers1

6

Thank you @reda-mattar, your link led me to the solution, here's what I was looking for:

var allCustomerRoles = (from cr in Customers
                        join r in Roles
                        on cr.Role_ID equals r.Role_ID
                        group r.Role_ID by cr.Customer_No_ into g
                        select new { Customer_No_ = g.Key, Role_ID = string.Join(",", g.ToArray()) });
Joe C
  • 137
  • 1
  • 1
  • 9