Deleting Items in List

3

I have a personal: List<Grupos> lista = new List<Grupos>();

And I have this code

var matches = lista.FindAll(x => x.Nome_Grupo.ToLower().Contains(txtFiltro.Text)).ToList();

To filter only words from my TxtFilter , I'd like to filter all items that do not have the word that I put in TxtFilter and if possible put more than one word.

Example bring all items that do not contain: pineapple, strawberry.

    
asked by anonymous 12.05.2016 / 01:22

2 answers

3

To filter you can use the Where method. .

var matches = lista.Where(x => !x.Nome_Grupo.ToLower().Contains(txtFiltro.Text)).ToList();

To filter more than one value you can use a List and check if the value you are filtering exists inside it.

var filtros = new List<string>() {"abacaxi", "morango"};
var matches = lista.Where(x => !filtros.Contains(x.Nome_Grupo.ToLower())).ToList();
    
12.05.2016 / 01:31
3

From what I understood the question, that's it

var matches = lista.Where(x => !x.Nome_Grupo.ToLower().Contains("abacaxi") &&
                               !x.Nome_Grupo.ToLower().Contains("morango")).ToList();

If you need to validate many values, it would be best to put them in a collection and then validate them, thus (assuming all words are in the textbox, separated by commas - no spaces).

var palavrasFiltro = txtFiltro.Text.ToLower().Split(',');
var matches = lista.Where(x => !palavrasFiltro.Contains(x.Nome_Grupo.ToLower())).ToList();

See an example in .NET Fiddle

    
12.05.2016 / 01:31