Find specific data within a List

1

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

datetime DataCotacao
double ValorCotação
double FatorDiario

Following 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 make a logic that from a condition where with two parameters of an StartDate and a EndDate, 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 that 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 the accuracy of the values here is the least important. I would really like to know the logic that I should implement to arrive at this final result (1.112906) taking all the conditions presented above.

    
asked by anonymous 05.12.2017 / 16:56

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 those records that are within the date range that you specified.

This can be done by using the Where of LINQ.

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

Note that using the Aggregate method will also enable 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 it is only to make the values of a to be saved somewhere.

Code sample:

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

See working in .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;
}
    
05.12.2017 / 17:27