简体   繁体   中英

does not contain in an ObservableCollection

Hi there ive been wrecking my head all night and I know I'm missing something simple. Please Help!

I have an observable collection called 'TakenSlots'. I want to see what integers it does not contain.

It works fine when I use the normal contains() method but does not return anything with the !contains() method.

            int y =1;
            foreach(item in TakenSlots)
            {
              if (!(TakenSlots.Contains(y)))
              {
                await DisplayAlert("Alert",y.ToString(),"Ok");
              }
               y++;
            }

your use of DisplayAlert is throwing you off - either it's not working because you're not on the main thread, or there is some other issue, but if you use a simple WriteLine statement it works as expected

ObservableCollection<int> data = new ObservableCollection<int>();

data.Add(1);
data.Add(2);
data.Add(4);

Console.WriteLine(!data.Contains(1));
Console.WriteLine(!data.Contains(2));
Console.WriteLine(!data.Contains(4));
Console.WriteLine(!data.Contains(5));

returns

false
false
false
true

Solved! For some reason it does not work with a foreach loop.

 for (int i = 1; i <= 8; i++)
            {


                if (!(TakenSlots.Contains(i)))
                {
                    await DisplayAlert("Alert", "does not contain " + i, "Ok");
                }
                else
                {
                    await DisplayAlert("Alert", "does contain " + i, "Ok");
                }


            }

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