Error making a return in windows form c #

0

Someone could give me a hand, please, I'd like to know what I'm doing wrong for this giving this error when it returns. I want to show in Grid these fields:

SubCategoriaId, SubCategoriaNome, CategoriaId, CategoriaNome

I have a parameter string valor to do a search and return these fields, if the value is blank, it brings me all of the bank's.

I created a Model, to have the results "typed":

public class ResultadoCategSubCategoriaGrid
{
   public int SubCategoriaId { get; set; }
   public string SubCategoriaNome { get; set; }
   public int CategoriaId { get; set; }
   public string CategoriaNome { get; set; }        

}

And I also have the models of each one Category:

public class Categoria    
{        
   public int CategoriaId { get; set; }
   public string Nome { get; set; }
}

And Subcategory

public class SubCategoria
{        
    public int SubCategoriaId { get; set; }
    public string Nome { get; set; }
    public int CategoriaId { get; set; }        
}

    
asked by anonymous 22.06.2016 / 10:42

1 answer

2

What happens is that your LINQ statement returns an anonymous type .

var minhaQuery = from su in _contexto.SubCategorias
                 join c in _contexto.Catgorias on su.CategoriaId equals c.CategoriaId
                 select new { SubCategoriaId = su.SubCategoriaId, SubCategoriaNome = su.Nome, CategoriaId = su.CategoriaId, CategoriaNome = c.Nome};

Instead, return the type you want by your method, that is, ResultadoCategSubCategoriaGrid .

It would look like this:

var minhaQuery = from su in _contexto.SubCategorias
                 join c in _contexto.Catgorias on su.CategoriaId equals c.CategoriaId
                 select new ResultadoCategSubCategoriaGrid() { SubCategoriaId = su.SubCategoriaId, SubCategoriaNome = su.Nome, CategoriaId = su.CategoriaId, CategoriaNome = c.Nome};

See if it works, at the moment I have no way to test the code.

    
22.06.2016 / 12:31