Fill a listbox with predicate<string>

Asked

Viewed 122 times

0

I am studying DDD and Webapi and as I find difficulties, I research and try to solve. Of course, sometimes I come across simple things for some here and I can’t get it right away.

Sometimes I even understand what has to be done, but I don’t know how to do it, as is the case now. With Console project, I did, but I switched to a Windows Form and I’m having this difficulty.

I need to fill a Listbox as a result of a lambda Foreach resulting from a list of a Predicate. The question is in Foreach, so:

private void button1_Click(object sender, EventArgs e)
{
    string[] teste = new string[] { "um", "dois", "tres", "quatro", "cinco" };
    Predicate<string> p = objetoTeste => objetoTeste.Length >= 4;

    var i = from items in teste
            where (p(items))
            select (items);

    i.ToList().ForEach(listBox1.Items.Add()**==>> Dúvida aqui**);

    /*foreach (var item in teste)
    {
        listBox1.Items.Add(item);
    }*/
}

The question is how to call the ListBox1.Items.Add() and fill it inside the foreach

1 answer

3


Thus

i.ToList().ForEach(item => listBox1.Items.Add(item));

If the method parameter Add is of the same type as item (string, in this case) you can still make it simpler

i.ToList().ForEach(listBox1.Items.Add);

See working . NET Fiddle.

Browser other questions tagged

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