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;
}
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.
– Brewerton Santos
@cabarbosa78 Its purpose is to arrive only at the final value or to obtain all the multiplications until arriving at the end?
– Jéf Bueno
@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..
– cabarbosa78
Got it, but what I want to know is: You need to know each of these values separately or just need the last?
– Jéf Bueno
I need only the final result (last).
– cabarbosa78