Find specific data within a List

Asked

Viewed 41 times

1

I have a List<Class> originated from a class. Class properties are:

datetime DataCotacao
double ValorCotação
double FatorDiario

Follow the data registered for example:

Data      FatorDiario
04/12/17   1,02077
05/12/17   1,03006
06/12/17   1,04563
07/12/17   1,03328

I would like to perform a logic that from a condition in which with two parameters of an initial date and a dataFinal, for example:

05/12/17 and 07/12/2017, the list returned the values of 1.03006, 1.04563 and 1.03328 and with these, I performed the multiplication in this order:

1º iteração: 1,03006
2º iteração: 1,03006 * 1,04563 = resultado em 1,0770616
3º iteração: 1,0770616 * 1,03328 = resultando em 1,112906 

And accuracy of values here is the least important. I would even like to know the logic that I should implement to arrive at this final result (1.112906) taking all the conditions presented above.

  • linear data structure - simply chained list is the type of structure that is being used, you can try to research on that surely finds the answer, or you can popular a dataset and use its data structure to pull the values.

  • @cabarbosa78 Its purpose is to arrive only at the final value or to obtain all the multiplications until arriving at the end?

  • @LINQ, the question is precisely that the final result is the composition of these multiplications based on the daily factors found in the list in the date range. I don’t know if I could enlighten you..

  • Got it, but what I want to know is: You need to know each of these values separately or just need the last?

  • I need only the final result (last).

1 answer

2


The solution to your problem can be divided into two distinct parts.

The first part is to create a filter to get only records that are within the date range that you specified.

This can be done using the extension method Where linq.

The second part of the solution consists of "accumulating" the results of each of the items that were filtered, so that the result is the multiplication of all of them. This can be done with the method Aggregate also from LINQ. You can see more about this method in the question How the Linq Aggregate() extension method works?

Understand that using the method Aggregate will also allow you to save all accumulated values. The variable a does the work of the accumulator, so if it is necessary to discriminate each of these values is just to make the values of a be saved somewhere.

Code example:

var res = _lista.Where(model => model.Data >= inicio && model.Data <= fim)
                .Select(model => model.FatorDiario)
                .Aggregate((a, b) => a * b);

See working on . NET Fiddle.

Full code, just run:

using System;
using System.Linq;
using System.Collections.Generic;

public class Program
{
    private static List<Model> _lista = new List<Model>
    {
        new Model { Data = new DateTime(2017, 12, 04), FatorDiario = 1.02077M },
        new Model { Data = new DateTime(2017, 12, 05), FatorDiario = 1.03006M },
        new Model { Data = new DateTime(2017, 12, 06), FatorDiario = 1.04563M },
        new Model { Data = new DateTime(2017, 12, 07), FatorDiario = 1.03328M }
    };

    public static void Main()
    {
        DateTime inicio = new DateTime(2017, 12, 05);
        DateTime fim = new DateTime(2017, 12, 07);

        var s = _lista.Where(model => model.Data >= inicio && model.Data <= fim)
                      .Select(model => model.FatorDiario)
                      .Aggregate((a, b) => a * b);

        Console.WriteLine(s);
    }
}

public class Model
{
    public DateTime Data;
    public decimal FatorDiario;
}
  • @LINK, thank you so much for the help. I will implement and see how the result looks. Getting ok, I return to finish. Thanks!

  • @LINK, the logic worked in my context, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.