Daniel Cruz Pablo
Daniel Cruz Pablo

Reputation: 13

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

Upvotes: 1

Views: 2230

Answers (2)

Emiliano Montesdeoca
Emiliano Montesdeoca

Reputation: 364

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.

Upvotes: 1

m4gik
m4gik

Reputation: 450

A generic list is not equal to List<td_encuestas>

Upvotes: 0

Related Questions