Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# limiting listbox items

I have this bit of code, which should be self explanatory:

    _item.Distance = Decimal.Round(dDistanceDec, 2);

    if (_item.Distance < 5)
    {
        tempItems.Add(_item);
    }
}

tempItems.OrderBy(i => i.Distance).ToList().ForEach(z => nearby.Items.Add(z));

(The bottom curly bracket closes a foreach loop if it makes a difference)

I am trying to limit the number of results to 10 in the 'nearby' listbox. I am a bit confused as it needs to sort them in order first of distance, but by doing that it is adding the items to the 'nearby' listbox. So where would the limiting code go?

like image 929
Dan Sewell Avatar asked Jan 19 '23 11:01

Dan Sewell


1 Answers

 tempItems.OrderBy(i => i.Distance)
           .Take(10)
           .ToList()
           .ForEach(z => nearby.Items.Add(z));
like image 79
Bala R Avatar answered Jan 29 '23 13:01

Bala R