Doubt in the Declaration of Type of Object - Entity Framework

2

I need help defining the correct class declaration

Follow my code:

public void BuscaOcorremcias()
{
    //Op 01 - Declarando um IList da Model
    IList<Ocorrencia> ocorrencias = null;
    //Op 02 - Declarando um DbSet
    System.Data.Entity.DbSet<MoradaWeb.Models.Ocorrencia> ocorrencias = null;

    ocorrencias = db.Ocorrencia.Include("Pessoa").Where(c => c.status == true);

    if (MinhaRegradeNegocio)
        ocorrencias = ocorrencias.Where(c => c.Pessoa == PessoaLogada);
}

// My Context

public class MeuContext : DbContext
{
    public MoradaWebContext() : base("name=MinhaConnectionString")
    {   ...   }

    public System.Data.Entity.DbSet<Models.Ocorrencia> Ocorrencia { get; set; }
}

The error that gives the two options above (in the code comment) is this:

  

Can not implicitly convert type ...

I'm not sure which type to declare so I can keep the condition of applying a Where else if I need to. In this case I want to make the second where within the collection itself that has already been searched in the database.

    
asked by anonymous 24.03.2017 / 20:18

1 answer

5

It is IQueryable what you need.

If you are only selecting DbSet ( db.Ocorrencia , in the example) it is also necessary to call the AsQueryable method because its type is just DbSet . From the first Include , Where or the like returned type becomes IQueryable .

You can see more details, in this my question .

IQueryable<Ocorrencia> ocorrencias = db.Ocorrencia.Include("Pessoa")
                                       .Where(c => c.status == true);

if (MinhaRegradeNegocio)
    ocorrencias = ocorrencias.Where(c => c.Pessoa == PessoaLogada);

You can also use var and let the type be set automatically.

    
24.03.2017 / 20:59