Determine the number of items in a list that are within the limits defined in another list

2

I have a ListBox that contains an undetermined value of values and I want to throw those values to a List<> and from what I researched, I managed to do so far:

var todosValores = lstRoll.Items.OfType<object>().Select(x => x.ToString()).ToList();
List<double> listaDeNums = todosValores.Select(s => double.Parse(s)).ToList();

var lista = new List<double>();
lista.Add(somaLinha1);
lista.Add(Math.Round((somaLinha1 + valorTamanhoIntervalo), 2));

The above code throws the values from ListBox to List<> and then plays to another List<> converting% values to%. It turns out that I now have another double distinct from those containing decimal numbers.

Assuming my List now loads the values listaDeNums and my other list (variable { 1,2,3,...,9,10 } in the code) load the values lista .

These two values need to be treated as the beginning and end of a range that will intersect in { 2.86 , 5.65 } and a variable is incremented to each number that is part of the intersection.

In this particular case exemplified by me above my variable incremented at the end, I should give 3 > and 5.65 in a list of 1 a 10     

asked by anonymous 25.03.2018 / 05:20

1 answer

2

If in the "other list", item 0 is the lower limit and item 1 is the upper limit, the code below builds a list of values within those limits.

var lista = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var limites = new List<double>{ 2.86, 5.65 };

var ocurrencias = new List<int>();
ocurrencias = lista.Where(valor => valor >= limites[0] && valor <= limites[1]).ToList();

If you want to know only the number of items in the list that are in the range use:

var lista = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var limites = new List<double>{ 2.86, 5.65 };
var numeroDeOcurrencias = lista.Count(valor => valor >= limites[0] && valor <= limites[1]);

See the .NET Fiddle .

    
25.03.2018 / 16:21