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 attempting to choose a few fields from a collection inside a collection. Roles->Users (name and ID) Select many allowed me to get flattened data, but now I must combine it back into a collection of objects so that my json is properly structured. If at all feasible, I would want to perform this using dynamic linq rather than manually merging the objects. I'd appreciate any assistance.

A many-to-many connection with User Role serving as the join table is User->User Roles->Roles.

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)")

These are the outcomes:

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

It ought to be

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

Popular Answer

To obtain the results you want, use GroupBy before SelectMany.

the following

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