Return missing date x days to arrive

0

Scenario:

Ciclos
id 
name
endDate (datetime?)

Data:

1, primeiro, 05/10/2017
2, segundo, 15/10/2017
3, terceiro, 20/10/2017
4, quarto, 30/10/2017

With linq and lambda, I would like to return dates that are above the current date, but are below a certain amount of days reported by the user.

Example if today is day 01/10/2017

The user informs 20 days, he should return the cycles 1, 2, 3 because these 3 records are in up to 20 days to "win"

This endDAte is an expiration date.

    
asked by anonymous 11.05.2017 / 22:35

1 answer

3

Let's break this down into three steps.

First, you should get the current date. After that, you add that date to the amount of days reported by the user and finally, just add it to your filter.

See the example below:

var listaCiclos = new List<Ciclos>(){
    new Ciclos() { id = 1, name = "primeiro", endDate = Convert.ToDateTime("05/10/2017") },
    new Ciclos() { id = 2, name = "segundo", endDate = Convert.ToDateTime("15/10/2017") },
    new Ciclos() { id = 3, name = "terceiro", endDate = Convert.ToDateTime("20/10/2017") },
    new Ciclos() { id = 4, name = "quarto", endDate = Convert.ToDateTime("30/10/2017") }
};


//Aqui eu adicionei a data de seu exemplo, mas você usaria Datetime.Now, para obter a data atual.
var dataAtual = Convert.ToDateTime("01/10/2017");

//Somamos a data atual mais a quantidade de dias digitado pelo usuário com o .AddDays()
var dataUsuario = dataAtual.AddDays(20);

//Filtramos a lista de acordo com os critérios informados
var listaFiltro = listaCiclos.Where(c => c.endDate > dataAtual && c.endDate < dataUsuario).ToList();

See a functional example in .NetFiddle

    
11.05.2017 / 22:53