I want to return a list of a join linq but I do not know how to do it, I do not know what goes in the List ????

0

I already try to create an object from the query of the join bone of the attributes that I want to see but still I can not make the return

    public List<????> ListaJoin()
    {
        var resultado = (from ad in model.AlumnoDireccion
                         join a in model.Alumno on ad.AlumnoId equals a.AlumnoId
                         select new { ad.Direccion, a.Nombre, a.Apellido, a.Estado, a.PromedioAcumulado }).ToList();

        return resultado();
    }
    
asked by Pignal 06.04.2017 в 21:06
source

3 answers

1

In the LINQ expression you are creating a anonymous type ( new { ... } ), these can only be used within the method in which they are declared, that is, you can not return an instance of this easily. The simplest thing is to create a class with only those properties to return the data you want.

public class AlumnoDireccion
{
    public string Direccion { get; set; }
    public string Nombre { get; set; }
    public string Apellido { get; set; }
    public string Estado { get; set; }        
    public double PromedioAcumulado { get; set }
}
public List<AlumnoDireccion> ListaJoin()
{
    return (from ad in model.AlumnoDireccion
            join a in model.Alumno on ad.AlumnoId equals a.AlumnoId
            select new AlumnoDireccion 
            { 
                Direccion = ad.Direccion,
                Nombre = a.Nombre,
                Apellido = a.Apellido,
                Estado = a.Estado,
                PromedioAcumulado = a.PromedioAcumulado
            }).ToList();
}
    
answered by 06.04.2017 / 22:02
source
0

For the case that you expose, use object . Personally I prefer to create a class that applies new MyClase and that return it. Enter the code here

    
answered by 06.04.2017 в 21:09
0

The solution that exposed @Equiso is the.mas suitable Only that I find a huge flaw When joins with Linq you must assign the join within a new object Because if for some. Reason the ID is not recorded in the second table, the record of the first will not appear and will not send you as null Tell eating change the query a little                          join a in model.Alumno on ad.AlumnoId equals a.AlumnoId into alumosObj                         From alumni in alumsObj.Defaulting empty () And any reference you make to the student table now make it to the students object for example StudentsObj.Name,

    
answered by 07.04.2017 в 04:25