I am trying to filter a set of data by any weekend, by using using a list of DateTimes, that is created dynamically. The list of DateTimes
, has several dates in it, that contain a from date and a to date. This can be for any weekend in the past.
The list of DateTimes looks similar to this:
public class ShiftTimeFilter
{
public DateTime ShiftTimeFrom { get; set; }
public DateTime ShiftTimeTo { get; set; }
}
And the dates that created are in a new List<ShiftTimeFilter>();
This creates the following example dates for a given weekend.
[0] ShiftTimeFrom : {23/01/2021 00:30}, ShiftTimeTo : {23/01/2021 16:30}
[1] ShiftTimeFrom : {24/01/2021 17:30}, ShiftTimeTo : {24/01/2021 19:30}
I can do it for one day, i.e. Saturday or a Sunday, but I cannot understand how I can do this using a list of DateTimes to incorporate both days and filter in one go.
The myData model is as follows:
public class myData
{
public DateTime ReadingDate { get; set; }
public double ReadingValue { get; set; }
}
The query I am using:
var data = myData.Where(x => x.ReadingDate.DayOfWeek == DayOfWeek.Saturday
&& x.ReadingDate > ShiftTimeFrom
&& x.ReadingDate <= ShiftTimeFrom )
.Select(x => new Data { myDate = x.ReadingDate, myVal = x.ReadingValue })
.ToList();
One of the problems I have is that the customer, may choose a period of time over several months, and they will want to see every weekend for the period of time. So the list of dates can have in some cases 10 or 20 dates in it.
I was hoping to use Linq to filter the dates by saying, give me all of the data that is between all of the dates List<ShiftTimeFilter>;
and anything that is not between those dates, I want to set it's value to zero.
TIA
question from:
https://stackoverflow.com/questions/65945878/linq-query-that-can-use-a-list-of-datetime-to-filter-a-listobject 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…