简体   繁体   中英

Data source skipping items while looping?

I've bound a datasource to a longlistselector and it works perfectly. I retrieve the data from a rather large nested json object, and it works. However, when I try to filter the items by one property called 'city' something weird happens. In my ViewModel I do this simple check:

private ObservableCollection<GigViewModel> _gigs;
public ObservableCollection<GigViewModel> Gigs
{
    get
    {
        for (int i = 0; i < _gigs.Count; i++)
        {
            if (_gigs[i].City == "venlo")
            {
                _gigs.Remove(_gigs[i]);
            }
        }
        return _gigs;
    }
    private set
    {
         if (value != _gigs)
         {
             _gigs = value;
         }
    }
}

So when the city string equals 'Venlo' I want it removed. The City string can be one of two things, it's either 'Sittard' or 'Venlo'. When I filter the list by the word 'Sittard' the output is correct and it only shows items from Venlo (this is a larger part of the list), but when I filter by 'Venlo' the list still contains a few items that have the city string as 'Venlo'.

When I try to debug, the longer I wait the more the list DOES get filtered, and after awhile the list is totally filtered and correct. But when I run it without a breakpoint, the list doesn't get filtered properly. Any explanation for this weird behavior?

You might be having a problem because you are deleting items from the list within the loop that is using _gigs.Count as the upper-limit of the loop. I'm not sure if this value is re-evaluated each time the loop is executed, but if not then it might be giving you weird behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM