Error doing a Return in windows form c#

Asked

Viewed 48 times

0

Could someone give me a hand, please, I’d like to know what I’m doing wrong so this giving this error at the time of return. I want to show on Grid these fields:

SubCategoriaId, SubCategoriaNome, CategoriaId, CategoriaNome

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

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 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; }        
}

inserir a descrição da imagem aqui

1 answer

2

What happens is that your instruction LINQ returns a 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 desired by your method, i.e., ResultadoCategSubCategoriaGrid.

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.