Deleting items in List

Asked

Viewed 89 times

3

Staff I have a: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 mine Txtfiltro, I would like to filter all items that do not have the word I put in Txtfiltro and if possible put more than one word.

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

  • You will put all these words in the textbox?

  • Managed to solve the problem?

2 answers

3

To filter you can use the method Where.

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

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

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

3

From what I understand of 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 better to put them in a collection and then validate, thus (taking into account that 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 on . NET Fiddle

  • tried here but it didn’t work, I used the second example with Split. I tested with 1 word and with more than one.

  • Didn’t work as? What happened? I’m without windows to test.

  • See an example working here https://dotnetfiddle.net/hFVgJQ

  • I tested your dotnetfiddle example and it worked, I think I’m in trouble because I have words that have uppercase and minuscule the ideal was to turn everything to uppercase and make the filter, correct? You can ignore the case sensitive?

  • Do you have words with uppercase and lowercase letters where? In the textbox? If so, do txtFiltro.Text.ToLower().Split(',');

  • I don’t know what else happens here on my app doesn’t work. I have a sentence with a few words, "Contains" had to work, but is not obeying.

  • Show the code, young man. If you don’t know what’s going on, imagine me, I can’t even see your code.

  • Look at the example with phrases. https://dotnetfiddle.net/aZvenw

  • Sentences won’t really work. You see, the word noivas contains the phrase noivas de plantão? Not, is the other way around, noiva is contained in noivas de plantão. If you had said you would work with sentences, it would change the whole direction of the answers.

  • Got it, open another question then?

  • There must be a smaller and simpler way to do this, but you get the result you want: https://dotnetfiddle.net/6V8zQ4. I’m not going to put this code in the answer because that’s not what you asked...

Show 6 more comments

Browser other questions tagged

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