开发者

c# limiting listbox items

开发者 https://www.devze.com 2023-03-13 15:51 出处:网络
I have this bit of code, which should be self explanatory: _item.Distance = Decimal.Round(dDistanceDec, 2);

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.O开发者_开发问答rderBy(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?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消