Home > Mobile >  How to display all numbers greater than 1 from List on ListView c#
How to display all numbers greater than 1 from List on ListView c#

Time:10-16

I want to display all numbers greater than 1 from my list on ListView

foreach (var item2 in listAlert)
{
      int maxAlertt = item2.levelForecast;
      item2.levelForecast = Math.Max(maxAlertt, maxAlertt);
      lstLevel2.ItemsSource = listAlert;
}

listAlert have data with integers 1,2,3,4 I want to display only 2,3 and 4 in lstLevel2.

How to do that ?

CodePudding user response:

you can do this with a LINQ query - you don't need a foreach loop

lstLevel2.ItemsSource = listAlert.Where(x => x.SomeProperty > SomeValue).ToList();

CodePudding user response:

You're overwriting your lstLevel2.ItemsSource with each iteration. You want to split it up:

var list = new List<myItem>();
foreach (var item2 in listAlert)
{
    if (item2.Data > 1)
    {
        list.Add(item2);
    }
}
lstLevel2.ItemsSource = list;
  • Related