Query Linq C #

1

Good afternoon

I have the following database structure:

Tables:

-User

-Group

- Permission

And all relations N to N:

  • User Group

  • User Permission

  • PermissionGroup

I made a query to return the user, regardless of Groups and Permissions (LEFT JOIN) with Linq in C #.

Follows:

tbUsuario = (from _u in _authEntities.tb_usuario
                          join _gu in _authEntities.tb_grupo_usuario on _u.id_tb_usuario equals _gu.id_tb_usuario
                          into u
                          from usuario in u.DefaultIfEmpty()
                          join _pu in _authEntities.tb_permissao_usuario on _u.id_tb_usuario equals _pu.id_tb_usuario
                          into p
                          from permissao in p.DefaultIfEmpty()
                          where _u.login == login && _u.senha == senha
                          select _u).SingleOrDefault();

What I need to include in this query I already have, plus a LEFT JOIN, for the Group Permission table.

If it were by SQL query it would be simple, but with LINQ I still have not found the solution. I await.

    
asked by anonymous 30.01.2017 / 14:57

1 answer

0

Try to make relationships as follows.

tbUsuario = (from _u in _authEntities.tb_usuario
                          join _gu in _authEntities.tb_grupo_usuario on _u.id_tb_usuario equals _gu.id_tb_usuario
                          into u
                          from usuario in u.DefaultIfEmpty()
                          join _pu in _authEntities.tb_permissao_usuario on _u.id_tb_usuario equals _pu.id_tb_usuario
                          into p
                          from permissao in p.DefaultIfEmpty()

                          join g in _authEntities.tb_grupo on u.id_tb_grupo equals g.id_tb_grupo
                          into g
                          from Grupo in g.DefaultIfEmpty()

                          join per in _authEntities.tb_permissao on per.id_tb_permissao equals p.id_tb_permissao
                          into per
                          from permi in per.DefaultIfEmpty()

                          join pergru in _authEntities.tb_Permissao_Grupo on new { Grupo.id_tb_grupo, permi.id_tb_permissao } equals new { pergru.id_tb_grupo, pergru.id_tb_permissao }
                          into pergru
                          from Permissao_Grupo in pergru.DefaultIfEmpty()


                          where _u.login == login && _u.senha == senha
                          select _u).SingleOrDefault();

I did not test them to see if the syntax is correct, have this as an example of how to do it.

    
30.01.2017 / 15:26