简体   繁体   中英

Return type from query using Entity Framework

Error:

Convert implicitly system.collections.generic.list return data query

My code:

public List<td_encuestas> getEncPreg(int userId)
{
    db.Configuration.LazyLoadingEnabled = false;

    var encuesta = (from enc in db.td_encuestas
                    join pre in db.td_preguntas on enc.enc_id equals pre.pre_enc_id
                    join res in db.td_respuestas on pre.pre_enc_id equals res.res_id
                    where enc.enc_activo == "true"
                        && pre.pre_activo == "true"
                        && enc.enc_usr_id_registro == userId
                    orderby enc.enc_descripcion
                    select new
                        {
                            enc,
                            pre,
                            res
                        }).ToList();

    return encuesta;
}

Return collection and relationship

通用列表不等于List<td_encuestas>

The Linq procedure that you are using does not return a List of that type/object, you should use a dynamic method , which returns something without knowing what it is, here's the code:

public dynamic List<td_encuestas> getEncPreg(int userId)
{
    db.Configuration.LazyLoadingEnabled = false;

    var encuesta = (from enc in db.td_encuestas
                    join pre in db.td_preguntas
                    on enc.enc_id equals pre.pre_enc_id
                    join res in db.td_respuestas
                    on pre.pre_enc_id equals res.res_id
                    where enc.enc_activo == "true"
                    && pre.pre_activo == "true"
                    && enc.enc_usr_id_registro == userId
                    orderby enc.enc_descripcion
                    select new
                    {
                        enc,
                        pre,
                        res
                    }).ToList();

    return encuesta;
}

And to use it:

var obj = getEncPreg(someId);

Documentation .

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