Get null bank fields with LINQ

4

I have a Gestor table in the database where I store information for a user with Manager profile. In this table, I have two FK's : uniaoId and schoolId . Well, this manager MUST belong to a union and may or may not belong to a school. That's my problem. At the time I join with linq, it does not bring managers who do not belong to a school. I've tried the following, but it did not work:

var listaRelatorioEmail = (
    from ge in db.Gestor
        join uni in db.Uniao on ge.UniaoId equals uni.UniaoId into uni_join
    from uni in uni_join.DefaultIfEmpty()                                               
        join es in db.Escola on ge.EscolaId equals es.EscolaId into es_join
    from es in es_join.DefaultIfEmpty()                                                 
        select new
        {
            UniaoNome = uni.Nome,
            UniaoId = uni.UniaoId,
            EscolaNome = es.Nome,
            EscolaId = es.EscolaId,
        }).ToList();
    
asked by anonymous 14.08.2015 / 14:27

2 answers

3

I chose to do the same thing with Extension Methods . It worked perfectly with the following code:

var listaRelatorioEmail = db.Gestor.Select(a => new
                             {
                                 UniaoNome = a.Nome,
                                 UniaoId = a.UniaoEntidadeId,
                                 EscolaNome = a.Nome,
                                 EscolaId = a.EscolaId == null ? 0 : a.EscolaId.Value
                             }).ToList();
    
14.08.2015 / 16:52
-1

I do not know if I understood better, try to change the join of the school to left join so he after all the managers and if n has school he puts null if I am not mistaken

var listaRelatorioEmail = (
from ge in db.Gestor
    join uni in db.Uniao on ge.UniaoId equals uni.UniaoId into uni_join
from uni in uni_join.DefaultIfEmpty()                                               
    **left join** es in db.Escola on ge.EscolaId equals es.EscolaId into es_join
from es in es_join.DefaultIfEmpty()                                                 
    select new
    {
        UniaoNome = uni.Nome,
        UniaoId = uni.UniaoId,
        EscolaNome = es.Nome,
        EscolaId = es.EscolaId,
    }).ToList();
    
14.08.2015 / 15:34