0

In my first question I didn't explain well. But now I need to select some columns from a entity framework class.

var Muestra = Cecytec.asignatura.Select(Z => new asignatura { nombre = Z.nombre, horasPorSemana = Z.horasPorSemana, nivel = Z.nivel, unidades = Z.unidades }).ToList();

From my class(table) "asignatura" I have this:

public partial class asignatura
{
    public asignatura()
    {
        this.criterioevaluacion = new HashSet<criterioevaluacion>();
        this.evaluacion = new HashSet<evaluacion>();
        this.examen = new HashSet<examen>();
        this.alumno = new HashSet<alumno>();
        this.horario = new HashSet<horario>();
        this.profesor = new HashSet<profesor>();
    }

    public int idAsignatura { get; set; }
    public Nullable<int> horasPorSemana { get; set; }
    public string nombre { get; set; }
    public Nullable<int> nivel { get; set; }
    public Nullable<int> unidades { get; set; }
    public int semestres_idsemestres { get; set; }

    public virtual semestres semestres { get; set; }
    public virtual ICollection<criterioevaluacion> criterioevaluacion { get; set; }
    public virtual ICollection<evaluacion> evaluacion { get; set; }
    public virtual ICollection<examen> examen { get; set; }
    public virtual ICollection<alumno> alumno { get; set; }
    public virtual ICollection<horario> horario { get; set; }
    public virtual ICollection<profesor> profesor { get; set; }
}

And I want to show: 'nombre', 'horasPorSemana', 'nivel', 'unidades' and 'calificacion'

Note: 'calificacion' is in another class

public partial class evaluacion
{
    public int unidad { get; set; }
    public Nullable<double> calificacion { get; set; }
    public Nullable<int> inasistencia { get; set; }
    public string observaciones { get; set; }
    public int asignatura_idAsignatura { get; set; }

    public virtual asignatura asignatura { get; set; }
}

2 Answers 2

2

In your projection you're projecting to an entity from your database. What you need to do is one of two options:

  1. Project to an anonymous class

    var Muestra = Cecytec .asignatura .Select(Z => new { nombre = Z.nombre, horasPorSemana = Z.horasPorSemana, nivel = Z.nivel, unidades = Z.unidades, evaluacion_calificacion = Z.evaluacion.Select(e => e.calificacion) }) .ToList();

This will give you a list of anonymous objects stored in the variable Muestra with each containing an IEnumerable of calificacion values from the evaluacion table.

  1. Project to a predefined class with the properties you need

This is similar to the above but you just define a class to hold the properties.

public class ProjectionResult
{
    public string nombre { get; set; }
    public Nullable<int> horasPorSemana { get; set; }
    public Nullable<int> nivel { get; set; }
    public Nullable<int> unidades { get; set; }
    public IEnumerable<Nullable<double>> evaluacion_calificacion { get; set; }
}

Then run your projection like this:

var Muestra = Cecytec
    .asignatura
    .Select(Z => new ProjectionResult
        {
            nombre = Z.nombre,
            horasPorSemana = Z.horasPorSemana,
            nivel = Z.nivel,
            unidades = Z.unidades,
            evaluacion_calificacion = Z.evaluacion.Select(e => e.calificacion)
        })
    .ToList();

This will give you a list of ProjectionResult objects stored in the variable Muestra with each containing an IEnumerable of calificacion values from the evaluacion table.

Sign up to request clarification or add additional context in comments.

2 Comments

OMG! the second works! Thank you Mr Member from Stackoverflow
@GertArnold correct, thank you. I've modified the answer.
0

Your answer is OK but it do not show

evaluacion_calificacion = Z.evaluacion.Select(e => e.calificacion)

Always null, to fix that I did this:

public class ProjectionResult { public Nullable<int> horasPorSemana { get; set; }
    public string nombre { get; set; }
    public Nullable<int> nivel { get; set; }
    public Nullable<int> unidades { get; set; }
    //public virtual IEnumerable<Nullable<double>> evaluacion_calificacion { get; set; }
    public Nullable<double> calificacion { get; set; } }

And to the query change the last line:

 var Muestra = Actualmente.Select(Z => new ProyeccionAcademica
                {
                    nombre = Z.nombre,
                    horasPorSemana = Z.horasPorSemana,
                    nivel = Z.nivel,
                    unidades = Z.unidades,                        
                    calificacion = Z.evaluacion.First(Find => Find.asignatura_idAsignatura == Z.idAsignatura).calificacion                        
                    }).ToList();

But thanks for your quick answer!!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.