Dan Sewell
Dan Sewell

Reputation: 1290

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?

Upvotes: 4

Views: 759

Answers (2)

Amir Ismail
Amir Ismail

Reputation: 3883

try to use Take(10) extension method that will return just 10 items.

Upvotes: 3

Bala R
Bala R

Reputation: 108957

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

Upvotes: 6

Related Questions