4
Hello, I would like to know the best way to filter a list of objects using LINQ in C#, I have a list of objects called Measuring, each measurement has a Data property, which is the date that was registered.
What I wish to answer is:
- Check the absence of measurements in a given month.
- Whether there is no measurement in 2 or more consecutive months
- Whether a measurement is available every 3 months(independent of the range in days)
- Whether there is no measurement in 4 or more consecutive months
For those who care, imagine the following:
List<Medicao> medicoes = Banco.CarregarListaMedicoes();
The Medical object could be for example:
public class Medicao {
public int Id { get; set; }
public DateTime Data { get; set; }
}
I want to filter the list above using LINQ and following the criteria I passed. Do you have any structure that makes this easier? Suggestions?
Put a minimal example of the objects. http://lorefnon.me/plain-text-table/
– rubStackOverflow
The object structure does not matter in this case, it can be any object and what you should only consider is that it has a Data attribute which is what will be used in the filter.
– rodrigorf
Do you just want to check if you have any of the cases? true/false
– Randrade
It’s not about importing or not, the question is that it facilitates the answer see how How to create a Minimum, Complete and Verifiable example
– rubStackOverflow
Exactly @Randrade
– rodrigorf
These measurements will have some initial or final date, or will be random?
– Randrade
I added a @rubStackOverflow change there.
– rodrigorf
var userMedictions = measurements. Where(x => x.data >= start & x.data <= end). Tolist<Medicao>(); Do not forget "using System.Linq;".
– Andre Mesquita
The measurements are recorded randomly by a person, what I need to ensure is that it meets those points, so that it does not occur to spend much time without registration, which generates a problem.
– rodrigorf
@Andremesquita Does this code solve the first topic only correct? How would you narrow it down to test the other cases?
– rodrigorf
In fact it does not solve. It is bringing all measurements within a period. It indicates how to mount the lambda.
– Andre Mesquita
Got it, in case I need to actually check for every month if you have absence. That is, if you have 12 months that the system is operating, I would like to make a LAMBDA expression that checks for each month if you have at least one measurement. If in every month you have but in December someone forgot to measure, then return false to me.
– rodrigorf
I did not understand what the question is. Wondering what would be the query to meet these 4 criteria?
– Maniero
This @bigown, it doesn’t have to be just a query that solves the 4 at once, if it works well, but if it gets better 4 separate methods could also be. Help me already
– rodrigorf