Contains within a list

0

I have a List of Assets that contains a list of Accessories

I would like to filter all the Assets that the accessories (ID_ACESSORIES) are inside an int list.

Follow 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));
    
asked by anonymous 08.03.2017 / 17:49

3 answers

1

Hello, try the following:

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

I hope I have helped.

    
08.03.2017 / 17:52
1

If you do not 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();
    
08.03.2017 / 18:03
1
var lAtivos = retorno.Where(c=> c.lAcessorios.where(cI => idAcessoriosFiltro.Any(cI.Id))).ToList();

return your list of assets, lAcessories the internal accessory list.

    
08.03.2017 / 18:05