简体   繁体   中英

How can I generate this query with Entity Framework to return the corresponding collections?

How can I generate this query with Entity Framework to return the corresponding collections that the question has only one theme.

select * 
from TD_ENCUESTAS as e 
join td_preguntas as p on e.ENC_ID = p.PRE_ENC_ID 
join TC_TEMAS as t on p.PRE_TEM_ID = t.TEM_ID;

在此处输入图片说明

Given that the models are correctly set-up, that would be quite easy:

var encuestas = context.Encuestas
    .Include(x => x.Preguntas.Tema)

For this to work, you would need:

public class Encuesta
{
    public virtual ICollection<Pregunta> Preguntas { get; set; }
}

public class Pregunta
{
    public Tema Tema { get; set; }
}

public class Tema
{
    // not actually needed but for clarification
    public virtual ICollection<Pregunta> Preguntas { get; set; }
}
using (DBEntities db = new DBEntities())
{
    var user = (from e in db.TD_ENCUESTAS
                join p in db.td_preguntas on e.ENC_ID equals p.PRE_ENC_ID
                join t in db.TC_TEMAS on p.PRE_TEM_ID equals t.TEM_ID;
                select new
                {
                   Name = e.ENC_DESCRIPCION,
                   Address = p.PRE_ACTIVO,
                   .....//So on...//
                });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM