0
I have the following code:
Code example, must debug and follow the variable "lstItem" to understand its functioning
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace TesteRotina
{
class Program
{
static void Main(string[] args)
{
List<Item> lstItem = new List<Item>
{
new Item
{
IdItem = 1,
NomeModificador = "ITEM 1",
DataUltimaMontagem = new DateTime(2015, 10, 18),
DiaMontagem = 18,
TipoPeriodicidade = (int) TipoPeriodicidadeEnum.Mensal
},
new Item
{
IdItem = 2,
NomeModificador = "ITEM 2",
DataUltimaMontagem = new DateTime(2015, 10, 31),
DiaMontagem = 31,
TipoPeriodicidade = (int) TipoPeriodicidadeEnum.Mensal
},
new Item
{
IdItem = 3,
NomeModificador = "ITEM 3",
DataUltimaMontagem = new DateTime(2015, 10, 29),
DiaMontagem = 29,
TipoPeriodicidade = (int) TipoPeriodicidadeEnum.Mensal
}
};
lstItem = FiltrarListaItem(lstItem);
}
private static List<Item> FiltrarListaItem(List<Item> lstItem)
{
List<Item> lstRetorno = new List<Item>();
foreach (var item in lstItem)
{
var dataUltimaMontagem = item.DataUltimaMontagem;
var diaMontagem = item.DiaMontagem;
var tipoPeriodicidade = item.TipoPeriodicidade;
switch (tipoPeriodicidade)
{
case (int)TipoPeriodicidadeEnum.Mensal:
dataUltimaMontagem = dataUltimaMontagem.AddMonths(1);
break;
case (int)TipoPeriodicidadeEnum.Bimestral:
dataUltimaMontagem = dataUltimaMontagem.AddMonths(2);
break;
case (int)TipoPeriodicidadeEnum.Trimestral:
dataUltimaMontagem = dataUltimaMontagem.AddMonths(3);
break;
case (int)TipoPeriodicidadeEnum.Semestral:
dataUltimaMontagem = dataUltimaMontagem.AddMonths(6);
break;
case (int)TipoPeriodicidadeEnum.Anual:
dataUltimaMontagem = dataUltimaMontagem.AddYears(1);
break;
}
if (dataUltimaMontagem.Day.Equals(DateTime.Now.Day) && dataUltimaMontagem.Month.Equals(DateTime.Now.Month))
lstRetorno.Add(item);
}
return lstItem;
}
}
public class Item
{
public int IdItem { get; set; }
public string NomeModificador { get; set; }
public int TipoPeriodicidade { get; set; }
public DateTime DataUltimaMontagem { get; set; }
public int DiaMontagem { get; set; }
}
public enum TipoPeriodicidadeEnum
{
[Description("Mensal")]
Mensal = 1,
[Description("Bimestral")]
Bimestral = 2,
[Description("Trimestral")]
Trimestral = 3,
[Description("Semestral")]
Semestral = 4,
[Description("Anual")]
Anual = 5
}
}
In the example, the method FiltrarListaItem
, removes all items, which have DataMontagem + periodiciodade > DataAtual
.
Ex.:
Datamontage = 10/19/2015 and Periodicity = Monthly.
Dating + Periodicity = 11/19/2015. This item would be disregarded as the result is greater than the current date(18/11/2015).
This is a routine that will be performed every day of the year.
How I make Items with Mount Day larger than the last day of the month to be considered and passed through the filter?
Ex.:
Measurement = 31 and the month only goes until 30.
Diamond cutting = 29, 30 or 31 and the month only goes until 28, as is the case of February.
The example does not work anything. I even tried to use it to try to understand where you are going, but I still don’t understand. Explain the problem better.
– Maniero
I will edit the question trying to explain the problem better.
– Jonathan Barcela
I think your answer is in your question .... How to do so when the assembly day is longer than the last day of the month..
dataUltimaMontagem.Day.Equals(DateTime.Now.Day)
why doesn’t itdataUltimaMontagem.Day <= DateTime.Now.Day
– Marco Souza
@Jonathanbarcela Se the
DiaMontagem
for 31/10 and the periodMensal
toUltimaMontagem
it has to be when? 30/11 or 01/12?– Jéf Bueno
@jbueno It has to be the same as the last day of the month.
– Jonathan Barcela
I modified your code the right way. Now it runs and it is possible to see the result. https://dotnetfiddle.net/49fWwE. I made a change, this is what you want?
– Maniero
@bigown This solution does not meet. I put an item with Dataultimamontage with the day 17/10/2015 and the mount date pro day 17 and it passed. I need to validate the assembly day too, disregarding only in the cases cited in the question(months.
– Jonathan Barcela
Man, only you can understand what you’re talking about.
– Jéf Bueno
So we still don’t understand the problem. You say "Datamontage + Periodicity = 19/11/2015. This item would be disregarded because the result is greater than the current date(18/11/2015).". Day 17 is not greater, so it passes. If the problem is not well defined, no solution will suit you. It’s not a code problem, it’s a rule problem. You don’t know what the rule is. I think I’d better close the question until you know what she is.
– Maniero
@bigown Note that the method takes the day into account. This is part of the rule, which is already defined. Also consider the part of the question where it says: "How I make Items with Mount Day larger than the last day of the month to be considered and pass through the filter. "
– Jonathan Barcela
@Jonathanbarcela think you better edit the question and make a list of the rules in a well organized way, really is impossible to understand what you need. The question is very confusing.
– Ricardo