In my foreach loop I would like to stop after 50 items, how would you break out of this foreach loop when I reach the 50th item?
Thanks
foreach (ListViewItem lvi in listView.Items)
int processed = 0; foreach(ListViewItem lvi in listView.Items) { //do stuff if (++processed == 50) break; }
or use LINQ
foreach( ListViewItem lvi in listView.Items.Cast<ListViewItem>().Take(50)) { //do stuff }
or just use a regular for loop (as suggested by @sgriffinusa and @Eric J.)
for(int i = 0; i < 50 && i < listView.Items.Count; i++) { ListViewItem lvi = listView.Items[i]; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With