我正在尝试在集合中的集合中选择几个字段。角色 - >用户(名称和ID)我能够使用select many获得扁平数据,但现在我需要将其合并回集合对象,以便我的json格式正确。如果可能的话,我想在动态linq中执行此操作,否则我可能必须手动将对象合并在一起。任何帮助都会很棒。
User-> User_Roles->角色(与User_Role的多对多关系作为连接表)
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)")
结果如下:
Role A-> User A
Role A-> User B ..notice the repeat of "Role A"
Role A-> User C
应该是
Role A -> User A
+ User B
+ User C
答案 0 :(得分:0)
我是JSON noob,但是假设你已经收集了一些项目
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
让我们说出来
UserRoleCollection
只需使用
var res = UserRoleCollection.GroupBy(item => item.Role);
你会得到IGrouping<TRole, TUser>
这样的
Role A -> User A
User B
User C
Role A -> User D
User E
User F
... and so on
答案 1 :(得分:0)
在SelectMany之前使用GroupBy来获取您正在寻找的结果。
类似的东西:
User_Roles.GroupBy(r => r.Roles.Role)
.Select(g => new { Role = g.Key, Users = g.SelectMany(u => u.Users.Name) });