我有一个具有用户->角色关系的 EF 模型。这是我们数据库中的一对多关系。我想从角色记录中选择 ID,从用户记录中选择名称。我想出的方法适用于一对一的关系。我不确定如何在动态 linq 中生成一个列表。也许一个SelectMany
?
用户表:用户
加入表:User_Role
角色表:角色
//does not select inside a collection of USER_ROLES
q= query.Select(" new ( ID, (new (USER_ROLES.ID as ID) as USER)")
//not valid
//q = query.Select(" new ( ID, (new List<Object> (USER_ROLES.ID as ID) as USER)") something I figure this would give me a list of User_Role's ID
//not valid
//q = query.Select(" new ( ID, (new List<Object> (USER_ROLES.ROLE.ID as ID) as USER)") something I figure this would give me a list of Role's ID
更新我越来越近了。我使用了一个selectman,但结果是重复的
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->User_Role A-> User A
User_Role A-> User B ..notice the repeat of "User_Role A"
User_Role A-> User C
它应该是
Role->User Role a -> User A
+ User B
+ User C