0

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
Nick Tullos
  • 511
  • 7
  • 23

2 Answers2

0

I'm a JSON noob, but lets say you allready have a collection of the items

Role A-> User A
Role A-> User B
Role A-> User C
Role B-> User D
Role B-> User E
Role B-> User F
Role C-> User G
Role C-> User H
Role C-> User I

Lets name it

UserRoleCollection

just use

var res = UserRoleCollection.GroupBy(item => item.Role);

You will get an IGrouping<TRole, TUser> looking like this

Role A ->     User A
              User B
              User C
Role A ->     User D
              User E
              User F
... and so on
Jan P.
  • 3,261
  • 19
  • 26
0

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) });
Doug Mitchell
  • 192
  • 1
  • 9