Contains within a list

Asked

Viewed 123 times

0

I have a list of Assets that contain a list of Accessories

I would like to filter all Assets that accessories(ID_ACESSORIOS) are within a list of int.

Follows the code;

//Lista com todos os ID_ACESSORIOS que tenho que filtrar
List<int> idAcessoriosFiltro = model.AcessoriosFiltroList.Where(a => a.Selecionado == true).Select(b => b.ID_ACESSORIO).ToList();

//A variavel retorno contem todos os ativos que preciso filtrar
List<Ativo> retorno = _Service.FiltrarAtivos(Mapper.Map<RelatorioAtivosFiltroViewModel, AtivoFiltro>(model));

3 answers

1


Hello, try the following:

 retorno.Where(w => 
     w.Acessorios.Any(a => idAcessoriosSelect.Contains(a.ID_ACESSORIO)
 ).ToList();

I hope I’ve helped.

  • No friend, I think I expressed myself wrong, come on. idAccesssSelect = Filter that comes from View with the id of the chosen accessories. return = Assets (database return) each asset has a list of accessories. each accessory has an ID_ACESSORIO, I need to pick up all the accessories whose id_ACESSORIO is inside mine (idAccessoriesSelect)

  • @Juniortorres you want to apply this filter between the idAccesssSelect and return lists, right?

  • (idAccessoriesFilter) is Filter (return) is the list that has to be filtered.

  • @juniortorres made adjustments in response.

  • Thank you very much!!

1

If you don’t have a restriction to use LINQ, maybe the expression below will help.

List<int> idAcessoriosSelect = (from f in model.AcessoriosFiltroList where f.Selecionado == true select f.ID_ACESSORIO).ToList();

1

var lAtivos = retorno.Where(c=> c.lAcessorios.where(cI => idAcessoriosFiltro.Any(cI.Id))).ToList();

return your asset list, lAccessories to internal accessories list.

Browser other questions tagged

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