Linq C # Outer Join Show the values that are not in the fields to compare

1

I have the following doubt, I hope you can help me. I have two tables, one of subjects and another that assigns the subjects to the students through their id. "Not all subjects have students assigned." They ask me to show with linq those subjects that do not have students assigned. In sql I would do it with outer join but in linq I do not know how to do it. I have the following code:

var matsin = from m in dbContext.Materias
                     join am in dbContext.AlumnoMaterias
                     on m.IdMateria equals am.IdMateria
                     where m.IdMateria != am.IdMateria
                     select new
                     {
                         Nombre = m.Nombre
                     };
    
asked by j bello 17.02.2018 в 22:22
source

1 answer

4

You have to use DefaultIfEmpty

Try the following

var matsin = from m in dbContext.Materias
                 join am in dbContext.AlumnoMaterias
                 on m.IdMateria equals am.IdMateria into mam
                 from am in mam.DefaultIfEmpty()
                 where am == null
                 select new
                 {
                     Nombre = m.Nombre
                 };

I did it by eye, if it fails somewhere, let it know!

Greetings

    
answered by 18.02.2018 / 03:08
source