How do I merge objects back together after a SelectMany in System.Linq.Dynamic or maybe different solution all together?

dynamic-linq linq linq-to-entities

Question

I'm trying to select out a couple of fields in a collection inside of collection. Roles->Users (name and ID) I was able to get flattened data using select many, but now I need to merge it back to a collection objects so my json is formatted correctly. I would like to do this in dynamic linq if possible otherwise I will might have to manually merge the objects back together. Any help would be great.

User->User_Roles->Roles (many to many relationship with User_Role as the join table)

q = query.SelectMany("USER_ROLES","new (inner as myUSER,outer as myROLE) ").SelectD("new (myROLE.ID as ROLE_ID, new( myROLE.NAME, myUSER.USER.FIRSTNAME,myUSER.USER.ID)as user)")

The results look like this:

 Role A-> User A
 Role A-> User B    ..notice the repeat of "Role A" 
 Role A-> User C

it should be

     Role A -> User A
             + User B
             + User C
1
0
10/2/2012 3:02:30 PM

Popular Answer

Use GroupBy before SelectMany to get the results you're looking for.

Something like:

User_Roles.GroupBy(r => r.Roles.Role)
            .Select(g => new { Role = g.Key, Users = g.SelectMany(u => u.Users.Name) });
0
10/2/2012 6:52:41 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow