How to call Linq method in View?

2

Good morning everyone. my question is this, I have the following method in my model:

public IQueryable GetConsulta()
{
    var q = (from c in Datacontext.Tabela1
                join b in Datacontext.Tabela2 on c.CodClase equals b.CodClase
                join d in Datacontext.Tabela3 on b.CodImobilizado equals d.CodImobilizado
                where c.CodClase == b.CodClase && b.CodImobilizado == d.CodImobilizado
                select new
                {
                    taxa = c.Taxa,
                    ano = DateTime.Parse(b.Data).Year,
                    data = d.Data,
                    valorAquisicao = b.Valor,

                });

    return q;
}

In View you look like this:

void getConsulta(){
    var a = model.GetConsulta();    
    foreach(var linha in a){
      //faça algo
    }
}

The problem is that I can not access the variables created in the model's foreach method.

Someone could help me.

    
asked by anonymous 26.08.2016 / 11:29

1 answer

2

The problem is with the return type you are using, in this approach, you do not specify the return of IQueryable, consequently in the view, you will not be able to see the desired data.

One suggestion would be to typify your return with a template class:

class Modelo {
    public string taxa { get; set; }
    public int ano { get; set; }
    public DateTime data { get; set; }
    public decimal valorAquisicao { get; set; }
}

public IQueryable<Modelo> GetConsulta()
{
    var q = (from c in Datacontext.Tabela1
                join b in Datacontext.Tabela2 on c.CodClase equals b.CodClase
                join d in Datacontext.Tabela3 on b.CodImobilizado equals d.CodImobilizado
                where c.CodClase == b.CodClase && b.CodImobilizado == d.CodImobilizado
                select new Modelo()
                {
                    taxa = c.Taxa,
                    ano = DateTime.Parse(b.Data).Year,
                    data = d.Data,
                    valorAquisicao = b.Valor,

                });

    return q;
}

In the View it would look like this:

void getConsulta(){
    var a = model.GetConsulta();    
    foreach(Modelo linha in a){
      //faça algo
    }
}
    
26.08.2016 / 15:05