Return Collection of Products according to Category

Asked

Viewed 149 times

1

I am unable to bring the list of products that has at least 1 related category.

//Classe

public class EventoConfiguracaoViewModel
    {

        public EventoConfiguracaoViewModel()
        {

            Menus = new List<MenuViewModel>();
            Categorias = new List<CategoriaViewModel>();
            Produtos = new List<ProdutoViewModel>();
            CupomDescontos = new List<CupomDesconto>();
            MenuLaterais =new List<MenuLateralViewModel>();
        }

        [Key]
        public int Id { get; set; }
        public string Nome { get; set; }
        public int CdEvento { get; set; }
        public virtual EventoViewModel Evento { get; set; }
        public virtual ICollection<MenuViewModel> Menus { get; set; }
        public ICollection<MenuLateralViewModel> MenuLaterais { get; set; }
        public virtual ICollection<CategoriaViewModel> Categorias { get; set; }
        public virtual ICollection<ProdutoViewModel> Produtos { get; set; }
        public virtual ICollection<CupomDesconto> CupomDescontos { get; set; }
    }

I did it that way and it didn’t work:

 public ActionResult ObterProdutoPorCategoria(int? id, string categoria, int? clienteId, int? cdEvento, string tema)
        {

            ViewBag.Tema = tema;
            var configuracao = _eventoConfiguracaoAppService.ObterTodosEventoConfiguracaosDoClientes(clienteId.Value,
                cdEvento.Value);

            var retv = configuracao.Where(e => e.Produtos.Any(p => p.CategoriaId == 2));

            return View(retv);


        }
  • Please try to be more specific. Click [Edit] and give more details. It is impossible to help you without knowing what you are talking about.

  • Like I’m not being specific right now. I informed in the text: "Staff I am unable to bring the list of products that has at least 1 related category."

  • Instead of print, put the code that makes it much easier to read. I believe it only takes the structure of the Configuration and Product class, and the code you are using today to perform the filter

  • Ready... I put the code

  • View takes which model?

  • Receive a Ienumerable<Eventoconfiguracaoviewmodel>

Show 1 more comment

1 answer

1


I drafted an answer that based on what you put....

I created a whole structure to make it easier to develop Basically what you need is this line, to return the list of products with the desired category

var idEquals1 = c.Where(e => e.Produtos != null && e.Produtos.Any(pe => pe.CategoriaId.Equals(2))).Select(a=> new Config{Produtos = a.Produtos.Where(p=>p.CategoriaId == 2).ToList()}).ToList();

Follow the program code for testing

class Program
{
    static void Main(string[] args)
    {

        List<Produto> p = new List<Produto>();
        List<Config> c = new List<Config>();

        var cat1 = new Categoria { Id = 1, Desc = "Categoria 1" };
        var cat2 = new Categoria { Id = 2, Desc = "Categoria 2" };

        for (int i = 0; i < 25; i++)
        {
            if (i % 2 == 0)
                p.Add(new Produto { Id = i, CategoriaId = 2, Desc = "produto " + i });
            else
                p.Add(new Produto { Id = i, CategoriaId = 1, Desc = "produto " + i });
        }

        c.Add(new Config { Produtos = prod });
        c.Add(new Config());


        var idEquals1 = c.Where(e => e.Produtos != null && e.Produtos.Any(pe => pe.CategoriaId.Equals(2))).Select(a=> new Config{Produtos = a.Produtos.Where(p=>p.CategoriaId == 2).ToList()}).ToList();


    }
}



public class Config
{

    public List<Produto> Produtos { get; set; }
}

public class Categoria
{

    public int Id { get; set; }
    public string Desc { get; set; }

}


public class Produto
{
    public int Id { get; set; }
    public int CategoriaId { get; set; }
    public string Desc { get; set; }
}
  • In this case it will return a Product object, but it would not be that, I would like it only to return Eventoconfiguration with only the products filtered by the category I determined var retv = configuration.Where(e => e.Produtos.Any(p => p.Categoriaid == 2));

  • I edited the answer now I think it solves your problem

  • 1

    you only need to fill in the new Event object

  • Okay I’ll try thank you

  • Marco you are the guy ... it worked out here many thanks for the attention ... saved the fatherland.. It was just that right there

Browser other questions tagged

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