Check number of Dates in Date Range that are sequential in c#? - c#

I'm trying to create a function that returns the number of Dates in a Date Range that are sequential, starting on a specific date.
Example:
StartDate: 9/1/2022
Date Range: 9/1/2022, 9/2/2022, 9/3/2022, 9/4/2022, 9/7/2022
In this scenario the function I'm looking for would return 4.
Assume dates could be unordered and they can roll over into the next month, so with StartDate 9/29/2022:
9/29/2022, 9/30/2022, 10/1/2022, 10/4/2022 would return 3.
I know I can loop through the dates starting at the specific date and check the number of consecutive days, but I'm wondering if there's a clean way to do it with Linq.

This is the cleanest solution I can come up with...
var startDate = new DateTime(2022, 9, 1);
var days = new List<DateTime>()
{
new(2022, 8, 28),
new(2022, 9, 1),
new(2022, 9, 2),
new(2022, 9, 3),
new(2022, 9, 4),
new(2022, 9, 7)
};
var consecutiveDays = GetConsecutiveDays(startDate, days);
foreach (var day in consecutiveDays)
{
Console.WriteLine(day);
}
Console.ReadKey();
static IEnumerable<DateTime> GetConsecutiveDays(DateTime startDate, IEnumerable<DateTime> days)
{
var wantedDate = startDate;
foreach (var day in days.Where(d => d >= startDate).OrderBy(d => d))
{
if (day == wantedDate)
{
yield return day;
wantedDate = wantedDate.AddDays(1);
}
else
{
yield break;
}
}
}
Output is:
01.09.2022 0:00:00
02.09.2022 0:00:00
03.09.2022 0:00:00
04.09.2022 0:00:00
If you wanted the count, you can call .Count() on the result or just modify the method... Should be easy.

To count the number of consecutive dates in a given date range.
first parse the dates from a string and order them in ascending order.
Then, use the TakeWhile method to take a sequence of consecutive dates from the start of the list.
Finally, count the number of elements in the returned sequence and display the result.
public class Program
{
private static void Main(string[] args)
{
var dateRange = "9/29/2022, 9/30/2022, 10/1/2022, 10/4/2022";
var dates = dateRange
.Split(", ")
.Select(dateStr =>
{
var dateData = dateStr.Split("/");
var month = int.Parse(dateData[0]);
var day = int.Parse(dateData[1]);
var year = int.Parse(dateData[2]);
return new DateTime(year, month, day);
})
.OrderBy(x => x)
.ToList();
var consecutiveDatesCounter = dates
.TakeWhile((date, i) => i == 0 || dates[i - 1].AddDays(1) == date)
.Count();
Console.WriteLine(consecutiveDatesCounter);
}
}
Output: 3
Demo: https://dotnetfiddle.net/tYdWvz

Using a loop would probably be the cleanest way to go. I would use something like the following:
List<DateTime> GetConsecutiveDates(IEnumerable<DateTime> range, DateTime startDate)
{
var orderedRange = range.OrderBy(d => d).ToList();
int startDateIndex = orderedRange.IndexOf(startDate);
if (startDateIndex == -1) return null;
var consecutiveDates = new List<DateTime> { orderedRange[startDateIndex] };
for (int i = startDateIndex + 1; i < orderedRange.Count; i++)
{
if (orderedRange[i] != orderedRange[i - 1].AddDays(1)) break;
consecutiveDates.Add(orderedRange[i]);
}
return consecutiveDates;
}

Yet another approach using a loop. (I agree with the others that said a loop would be cleaner than using Linq for this task.)
public static int NumConsecutiveDays(IEnumerable<DateTime> dates)
{
var previous = DateTime.MinValue;
var oneDay = TimeSpan.FromDays(1);
int result = 0;
foreach (var current in dates.OrderBy(d => d))
{
if (current.Date - previous.Date == oneDay)
++result;
previous = current;
}
return result > 0 ? result + 1 : 0; // Need to add 1 to result if it is not zero.
}

var dates = new List<DateTime>() {new DateTime(2014,1,1), new DateTime(2014, 1, 2), new DateTime(2014, 1, 3) , new DateTime(2014, 1, 5), new DateTime(2014, 1, 6), new DateTime(2014, 1, 8) };
var startDate = new DateTime(2014,1,2);
var EarliestContiguousDates = dates.Where(x => x>=startDate).OrderBy(x => x)
.Select((x, i) => new { date = x, RangeStartDate = x.AddDays(-i) })
.TakeWhile(x => x.RangeStartDate == dates.Where(y => y >= startDate).Min()).Count();

You're either going to sort the dates and find sequential ones, or leave it unsorted and repeatedly iterate over the set looking for a match.
Here's the latter dumb approach, leaving it unsorted and using repeated calls to 'IndexOf`:
public static int CountConsecutiveDays(DateTime startingFrom, List<DateTime> data)
{
int count = 0;
int index = data.IndexOf(startingFrom);
while(index != -1) {
count++;
startingFrom = startingFrom.AddDays(1);
index = data.IndexOf(startingFrom);
}
return count;
}

Related

Reversing a list of objects with a timespan property, while maintaining the time difference between the objects

I'd like to reverse a list of objects with a TimeSpan property, which should maintain it's TimeSpan difference when reversing.
To give an example, consider a route from A to D with the following TimeSpans:
(A 12:00), (B 12:15), (C 12:40), (D 13:40).
Between A and B there is a 15 minute difference, between B and C there is a 25 minute difference and so on. I'd like to reverse this list in an efficient manner, where the result list would look like:
(D: 12:00), (C 13:00), (B 13:25), (A 13:40).
My first idea was creating a list of time differences and using that and the start time to create the new objects with the correct times, however I feel like the solution could be better.
Edit: Added my (working) sample solution. Any feedback is appreciated.
private IList<Activity> ReverseActivities(IList<Activity> activities)
{
IList<TimeSpan> timeDifferences = GetTimeDifferences(activities);
IList<Activity> resultList = new List<Activity>();
TimeSpan timeOfDay = activities.First().TimeOfDay;
for (int i = activities.Count - 1; i >= 0; i--)
{
resultList.Add(new Activity(activities[i].Name, timeOfDay));
timeOfDay = timeOfDay.Add(timeDifferences[i]);
}
return resultList;
}
private IList<TimeSpan> GetTimeDifferences(IList<Activity> activities)
{
IList<TimeSpan> timeDifferences = new List<TimeSpan>();
Activity prev = activities.First();
if (activities.Count > 1)
{
foreach (var curr in activities)
{
timeDifferences.Add(curr.TimeOfDay - prev.TimeOfDay);
prev = curr;
}
}
return timeDifferences;
}
Activity looks as follows:
public class Activity
{
public Activity(string name, TimeSpan timeOfDay)
{
this.Name = name;
this.TimeOfDay = timeOfDay;
}
public string Name { get; }
public TimeSpan TimeOfDay { get; }
}
One trick we can use is to have a single loop that finds the corresponding item from the end of the list based on the current index. We can do this like:
for (int i = 0; i < activities.Count; i++)
var correspondingIndex = activities.Count - i - 1;
Notice that:
When i is 0, correspondingIndex is the last index in the array.
When i is 1, correspondingIndex is the second-to-last index in the array.
When i is activities.Count - 1 (the last index), correspondingIndex is 0
Using this trick, we can get the corresponding time differences at the same time as we populate a new list of Activity objects.
Hopefully this code makes it a little clearer:
public static IList<Activity> ReverseActivities(IList<Activity> activities)
{
// If activities is null or contains less than 2 items, return it
if ((activities?.Count ?? 0) < 2) return activities;
// This will contain the reversed list
var reversed = new List<Activity>();
for (int i = 0; i < activities.Count; i++)
{
// Get the corresponding index from the end of the list
var correspondingIndex = activities.Count - i - 1;
// Get the timespan from the corresponding items from the end of the list
var timeSpan = i == 0
? TimeSpan.Zero
: activities[correspondingIndex + 1].TimeOfDay -
activities[correspondingIndex].TimeOfDay;
// The new TimeOfDay will be the previous item's TimeOfDay plus the TimeSpan above
var timeOfDay = i == 0
? activities[i].TimeOfDay
: reversed[i - 1].TimeOfDay + timeSpan;
reversed.Add(new Activity(activities[correspondingIndex].Name, timeOfDay));
}
return reversed;
}
In use, this would look like:
var original = new List<Activity>
{
new Activity("A", new TimeSpan(0, 12, 0)),
new Activity("B", new TimeSpan(0, 12, 15)),
new Activity("C", new TimeSpan(0, 12, 40)),
new Activity("D", new TimeSpan(0, 13, 40))
};
var reversed = ReverseActivities(original);
Here's the output in the debug window (compare original and reversed):
This is quite simple using a bit of TimeSpan maths.
IList<Activity> input = new List<Activity>()
{
new Activity("A", TimeSpan.Parse("12:00")),
new Activity("B", TimeSpan.Parse("12:15")),
new Activity("C", TimeSpan.Parse("12:40")),
new Activity("D", TimeSpan.Parse("13:40")),
};
TimeSpan min = input.Min(x => x.TimeOfDay);
TimeSpan max = input.Max(x => x.TimeOfDay);
IList<Activity> output =
input
.Select(x => new Activity(
x.Name,
x.TimeOfDay.Subtract(max).Duration().Add(min)))
.OrderBy(x => x.TimeOfDay)
.ToList();
That gives me:
I tested this and it works:
DateTime[] times = { new DateTime(2020, 06, 17, 12, 00, 00),
new DateTime(2020, 06, 17, 12, 15, 00), new DateTime(2020, 06, 17, 12, 40, 00),
new DateTime(2020, 06, 17, 13, 40, 00) };
List<DateTime> newTimes = new List<DateTime>();
newTimes.Add(times[0]);
for (int i = 1; i < times.Length; i++) {
DateTime d = newTimes[i - 1].Add(times[times.Length - i] - times[times.Length - i - 1]);
newTimes.Add(d);
}
Using LinkedList:
static void Main(string[] args)
{
var list = new List<Location>
{
new Location{Name = "A", TimeOffset = DateTimeOffset.MinValue.Add(new TimeSpan(12, 0, 0)) },
new Location{Name = "B", TimeOffset = DateTimeOffset.MinValue.Add(new TimeSpan(12, 15, 0)) },
new Location{Name = "C", TimeOffset = DateTimeOffset.MinValue.Add(new TimeSpan(12, 40, 0)) },
new Location{Name = "D", TimeOffset = DateTimeOffset.MinValue.Add(new TimeSpan(13, 40, 0)) },
};
var route = new LinkedList<Location>(list);
WriteToConsole("Before: ", route);
var reversedRoute = Reverse(route);
Console.WriteLine();
WriteToConsole("After: ", reversedRoute);
Console.WriteLine(); Console.ReadKey();
}
public static LinkedList<Location> Reverse(LinkedList<Location> route)
{
LinkedList<Location> retVal = new LinkedList<Location>();
DateTimeOffset timeOffset = DateTimeOffset.MinValue;
var currentNode = route.Last;
while (currentNode != null)
{
var next = currentNode.Next;
if (next == null)
{
// last node, use the first node offset
timeOffset = DateTimeOffset.MinValue.Add(route.First.Value.TimeOffset - timeOffset);
}
else
{
timeOffset = timeOffset.Add(next.Value.TimeOffset - currentNode.Value.TimeOffset);
}
retVal.AddLast(new Location { Name = currentNode.Value.Name, TimeOffset = timeOffset });
currentNode = currentNode.Previous;
}
return retVal;
}
public static void WriteToConsole(string title, LinkedList<Location> route)
{
Console.Write($"{title}: ");
foreach (var i in route)
{
Console.Write($"\t({i.Name}, {i.TimeOffset.Hour:D2}:{i.TimeOffset.Minute:D2})");
}
}

Group by date range , count and sort within each group LINQ

I have a collection of dates stored in my object. This is sample data. In real time, the dates will come from a service call and I will have no idea what dates and how many will be returned:
var ListHeader = new List<ListHeaderData>
{
new ListHeaderData
{
EntryDate = new DateTime(2013, 8, 26)
},
new ListHeaderData
{
EntryDate = new DateTime(2013, 9, 11)
},
new ListHeaderData
{
EntryDate = new DateTime(2013, 1, 1)
},
new ListHeaderData
{
EntryDate = new DateTime(2013, 9, 15)
},
new ListHeaderData
{
EntryDate = new DateTime(2013, 9, 17)
},
new ListHeaderData
{
EntryDate = new DateTime(2013, 9, 5)
},
};
I now need to group by date range like so:
Today (1) <- contains the date 9/17/2013 and count of 1
within 2 weeks (3) <- contains dates 9/15,9/11,9/5 and count of 3
More than 2 weeks (2) <- contains dates 8/26, 1/1 and count of 2
this is my LINQ statement which doesn't achieve what I need but i think i'm in the ballpark (be kind if I'm not):
var defaultGroups = from l in ListHeader
group l by l.EntryDate into g
orderby g.Min(x => x.EntryDate)
select new { GroupBy = g };
This groups by individual dates, so I have 6 groups with 1 date in each. How do I group by date range , count and sort within each group?
Introduce array, which contains ranges you want to group by. Here is two ranges - today (zero days) and 14 days (two weeks):
var today = DateTime.Today;
var ranges = new List<int?> { 0, 14 };
Now group your items by range it falls into. If there is no appropriate range (all dates more than two weeks) then default null range value will be used:
var defaultGroups =
from h in ListHeader
let daysFromToday = (int)(today - h.EntryDate).TotalDays
group h by ranges.FirstOrDefault(range => daysFromToday <= range) into g
orderby g.Min(x => x.EntryDate)
select g;
UPDATE: Adding custom ranges for grouping:
var ranges = new List<int?>();
ranges.Add(0); // today
ranges.Add(7*2); // two weeks
ranges.Add(DateTime.Today.Day); // within current month
ranges.Add(DateTime.Today.DayOfYear); // within current year
ranges.Sort();
How about doing this?
Introduce a new property for grouping and group by that.
class ListHeaderData
{
public DateTime EntryDate;
public int DateDifferenceFromToday
{
get
{
TimeSpan difference = DateTime.Today - EntryDate.Date;
if (difference.TotalDays == 0)//today
{
return 1;
}
else if (difference.TotalDays <= 14)//less than 2 weeks
{
return 2;
}
else
{
return 3;//something else
}
}
}
}
Edit: as #servy pointed in comments other developers may confuse of int using a enum will be more readable.
So, modified version of your class would look something like this
class ListHeaderData
{
public DateTime EntryDate;
public DateRange DateDifferenceFromToday
{
get
{
//I think for this version no comments needed names are self explanatory
TimeSpan difference = DateTime.Today - EntryDate.Date;
if (difference.TotalDays == 0)
{
return DateRange.Today;
}
else if (difference.TotalDays <= 14)
{
return DateRange.LessThanTwoWeeks;
}
else
{
return DateRange.MoreThanTwoWeeks;
}
}
}
}
enum DateRange
{
None = 0,
Today = 1,
LessThanTwoWeeks = 2,
MoreThanTwoWeeks = 3
}
and use it like this
var defaultGroups = from l in ListHeader
group l by l.DateDifferenceFromToday into g // <--Note group by DateDifferenceFromToday
orderby g.Min(x => x.EntryDate)
select new { GroupBy = g };
Do you specifically want to achieve the solution in this way? Also do you really want to introduce spurious properties into your class to meet these requirements?
These three lines would achieve your requirements and for large collections willbe more performant.
var todays = listHeader.Where(item => item.EntryDate == DateTime.Today);
var twoWeeks = listHeader.Where(item => item.EntryDate < DateTime.Today.AddDays(-1)
&& item.EntryDate >= DateTime.Today.AddDays(-14));
var later = listHeader.Where(item => item.EntryDate < DateTime.Today.AddDays(-14));
also you then get the flexibility of different groupings without impacting your class.
[Edit: in response to ordering query]
Making use of the Enum supplied above you can apply the Union clause and OrderBy clause Linq extension methods as follows:
var ord = todays.Select(item => new {Group = DateRange.Today, item.EntryDate})
.Union(
twoWeeks.Select(item => new {Group = DateRange.LessThanTwoWeeks, item.EntryDate}))
.Union(
later.Select(item => new {Group = DateRange.MoreThanTwoWeeks, item.EntryDate}))
.OrderBy(item => item.Group);
Note that I'm adding the Grouping via a Linq Select and anonymous class to dynamically push a Group property again not effecting the original class. This produces the following output based on the original post:
Group EntryDate
Today 17/09/2013 00:00:00
LessThanTwoWeeks 11/09/2013 00:00:00
LessThanTwoWeeks 15/09/2013 00:00:00
LessThanTwoWeeks 05/09/2013 00:00:00
MoreThanTwoWeeks 26/08/2013 00:00:00
MoreThanTwoWeeks 01/01/2013 00:00:00
and to get grouped date ranges with count:
var ord = todays.Select(item => new {Group = DateRange.Today, Count=todays.Count()})
.Union(
twoWeeks.Select(item => new {Group = DateRange.LessThanTwoWeeks, Count=twoWeeks.Count()}))
.Union(
later.Select(item => new {Group = DateRange.MoreThanTwoWeeks, Count=later.Count()}))
.OrderBy(item => item.Group);
Output is:
Group Count
Today 1
LessThanTwoWeeks 3
MoreThanTwoWeeks 2
I suppose this depends on how heavily you plan on using this. I had/have a lot of reports to generate so I created a model IncrementDateRange with StartTime, EndTime and TimeIncrement as an enum.
The time increment handler has a lot of switch based functions spits out a list of times between the Start and End range based on hour/day/week/month/quarter/year etc.
Then you get your list of IncrementDateRange and in linq something like either:
TotalsList = times.Select(t => new RetailSalesTotalsListItem()
{
IncrementDateRange = t,
Total = storeSales.Where(s => s.DatePlaced >= t.StartTime && s.DatePlaced <= t.EndTime).Sum(s => s.Subtotal),
})
or
TotalsList = storeSales.GroupBy(g => g.IncrementDateRange.StartTime).Select(gg => new RetailSalesTotalsListItem()
{
IncrementDateRange = times.First(t => t.StartTime == gg.Key),
Total = gg.Sum(rs => rs.Subtotal),
}).ToList(),

Sum difference in timespans, sorted by travel days per year

So I have all these travel dates, from-date and to-date. I want to add up all the travel days, and sort them by year. However if one travel for period which spans two years, my code will reach the wrong sum :-(
Given
From date To date Number of days
01.01.2001 01.02.2001 32
01.01.2002 01.02.2002 32
01.05.2002 01.08.2002 93
20.12.2002 01.03.2003 72
01.02.2009 01.02.2010 366
01.01.2013 02.02.2015 763
Sum 1358
My code produces this. However, it makes a mistake:
Year Total days
2001 32
2002 137
2003 60
2009 334
2010 32
2013 365
2014 398 <---- here is a case where my code is wrong
Sum 1358
Code
var dates = new Dictionary<int, int>();
var stays = GetStays();
var returnString = "Year, Total days<br><br>";
foreach (var stay in stays)
{
var totalTravelDays = stay.ToDate.Value.AddDays(1) - stay.FromDate;
var currentYear = stay.FromDate.Value.Year;
var nextYear = stay.FromDate.Value.AddYears(1).Year;
var nextYearDate = new DateTime(stay.FromDate.Value.Year, 1, 1).AddYears(1);
var daysInThisYear = new TimeSpan?();
var daysInNextYear = new TimeSpan?();
if (stay.FromDate.Value.Year != stay.ToDate.Value.Year)
{
daysInThisYear = nextYearDate - stay.FromDate;
daysInNextYear = totalTravelDays - daysInThisYear;
}
else
{
daysInThisYear = totalTravelDays;
daysInNextYear = new TimeSpan(0);
}
if (dates.ContainsKey(currentYear))
dates[currentYear] += daysInThisYear.Value.Days;
else
dates[currentYear] = daysInThisYear.Value.Days;
if (dates.ContainsKey(nextYear))
dates[nextYear] += daysInNextYear.Value.Days;
else
dates[nextYear] = daysInNextYear.Value.Days;
}
Help appreciated :)
Assuming that var stays = List<Stay>();, you may try this:
var days = stays.SelectMany(s =>
Enumerable
.Range(0, (s.ToDate - s.FromDate).Days + 1)
.Select(d => s.FromDate.AddDays(d)))
.GroupBy(d => d.Year)
.Select(s => new { Year = s.Key, TotalDays = s.Count() })
.ToList();
days.ForEach(d =>
{
Console.WriteLine("{0} {1}", d.Year, d.TotalDays);
});
The output of the above is:
2001 32
2002 137
2003 60
2009 334
2010 32
2013 365
2014 365
2015 33
If you write a helper method to split date ranges into several ranges partitioned by year:
IEnumerable<Tuple<DateTime,DateTime>>
SplitDateRangeByYear(DateTime fromDate, DateTime toDate)
{
var start = fromDate;
for(var y = fromDate.Year; y < toDate.Year; ++y)
{
var nextYear = y + 1;
var nextYearStartDate = new DateTime(nextYear, 1, 1);
yield return Tuple.Create(start, nextYearStartDate);
start = nextYearStartDate;
}
yield return Tuple.Create(start, toDate);
}
Then you can write some handy Linq to do your bidding:
var yearlyTotals = stays
.SelectMany(s => SplitDateRangeByYear(s.FromDate, s.ToDate))
.GroupBy(x => x.Item1.Year)
.Select(g => new{
Year = g.Key,
NumDays= g.Sum(x => (x.Item2 - x.Item1).TotalDays)});
This is a more general solution that you requested because it will properly deal with sub-day TimeSpan components (i.e. your ranges include times of day).
You don't need a so big code to calculate the days between two dates; you can rely on a much simpler/more accurate approach. For example:
DateTime date1 = new DateTime(2013, 1, 1);
DateTime date2 = new DateTime(2015, 2, 2);
int totDays = Math.Abs(date1.Subtract(date2).Days);
Adapted to your specific situation:
int[] days = new int[Math.Abs(date1.Year - date2.Year) + 1];
int curYear = 0;
if (date1.Year != date2.Year)
{
if(date1.Year > date2.Year)
{
DateTime temp = date1;
date1 = date2;
date2 = temp;
}
int curYearNo = date1.Year - 1;
curYear = -1;
do
{
curYearNo = curYearNo + 1;
curYear = curYear + 1;
if (curYearNo < date2.Year)
{
days[curYear] = Math.Abs(new DateTime(curYearNo, 1, 1).Subtract(new DateTime(curYearNo, 12, 31)).Days) + 1; //Without +1 it would output 364/365 (because of not including both 1st January and 31st December)
}
else
{
days[curYear] = Math.Abs(new DateTime(curYearNo, 1, 1).Subtract(date2).Days);
}
} while (curYearNo < date2.Year);
}
else
{
days[curYear] = Math.Abs(date1.Subtract(date2).Days);
}

Get data in time range pattern when data and time are in differents arrays

I have an array of data (double[] data) and a list of datetimes (List datetimes). Each position of data array is related to the position of the datetimes. I mean: data[i] was collected in datetimes[i].
Now I want to filter the data collected with a week pattern (7 day, 24 hours).
So, I have the week pattern:
class WeekPattern
{
List<DayPattern> week;
public WeekPattern(List<DayPattern> _week)
{
week = _week;
}
public bool isInRange(DateTime time)
{
return week.Any(i => i.isInRange(time));
}
}
class DayPattern
{
DayOfWeek day;
List<bool> hours;
public DayPattern(List<bool> _hours, DayOfWeek _day)
{
hours = _hours;
day = _day;
}
public bool isInRange(DateTime time)
{
if (time.DayOfWeek != day)
return false;
return hours[time.Hour];
}
}
Filter the datetimes in range is easy (I have alread Weekpattern pattern object)
double[] data = { 1, 2, 3, 4}
string[] times = { "23/01/2013 12:00", "23/01/2013 13:00", "23/01/2013 14:00", "23/01/2013 15:00" }
List<DateTime> datetimes = Array.ConvertAll(_times, time => DateTime.ParseExact(time, "dd/MM/yyyy HH:mm:ss", null)).ToList();
Weekpattern pattern... // Weekpattern object
List<DateTime> filter = datetimes.Where(i => pattern.isInRange(i)).ToList();
But, how I get the data filteres (double[] data filtered) instead of datetimes the list of datetimes filtered?
1 was collected on 23/01/2013 12:00
2 was collected on 23/01/2013 13:00
3 was collected on 23/01/2013 14:00
4 was collected on 23/01/2013 15:00
Suppose I have a range "Wednesday, 13:00 - 14:00". So I want to get an array of doubles with 2 and 3:
data = { 2, 3 }
Once you have the list of matching dates, simply call the IndexOf() function on the datetimes list for each match then use the return to pull the value out of the double[].
Sample:
var date = new DateTime(2013, 1, 12);
List<DateTime> dates = new List<DateTime>() { new DateTime(2013, 1, 11), date, new DateTime(2013, 1, 13) };
double[] values = new double[] { 0, 1, 2 };
var filtered = dates.Where(x => x == date);
foreach (var found in filtered)
{
Console.Write(values[dates.IndexOf(found)]);
}
Console.ReadLine();
You can try something like this (this overload of the Select method accepts the element index):
var filteredData = datetimes.Select((date, i) =>
{
if (pattern.isInRange(date))
{
return data[i];
}
else
{
return -1;
}
});
The only problem is that I'll need to verify if the value is equals to -1. But this works for me.
Editing: a better solution would be using the Where method overload that uses the element index on the lambda expression:
var filteredData = data.Where((d, i) =>
{
return pattern.isInRange(datetimes[i]);
});

Split a period of time into multiple time periods

If i have a time period, lets say DateFrom and DateTo and I have a list of Dates, These dates will be the split dates. For example:
DateTime dateFrom = new DateTime(2012, 1, 1);
DateTime dateTo = new DateTime(2012, 12, 31);
List<DateTime> splitDates = new List<DateTime>
{
new DateTime(2012,2,1),
new DateTime(2012,5,1),
new DateTime(2012,7,1),
new DateTime(2012,11,1),
};
List<Tuple<DateTime, DateTime>> periods = SplitDatePeriod(dateFrom, dateTo, splitDates);
I want the result to be a list of periods, so for the previous example the result should be:
(01/01/2012 - 01/02/2012)
(02/02/2012 - 01/05/2012)
(02/05/2012 - 01/07/2012)
(02/07/2012 - 01/11/2012)
(02/11/2012 - 31/12/2012)
I have already wrote a method to do that:
List<Tuple<DateTime, DateTime>> SplitDatePeriod(DateTime dateFrom, DateTime dateTo, List<DateTime> splitDates)
{
var resultDates = new List<Tuple<DateTime, DateTime>>();
// sort split dates
List<DateTime> _splitDates = splitDates.OrderBy(d => d.Date).ToList();
DateTime _curDate = dateFrom.Date;
for (int i = 0; i <= _splitDates.Count; ++i)
{
DateTime d = (i < _splitDates.Count) ? _splitDates[i] : dateTo;
// skip dates out of range
if (d.Date < dateFrom.Date || d.Date > dateTo.Date)
continue;
resultDates.Add(Tuple.Create(_curDate, d));
_curDate = d.AddDays(1);
}
return resultDates;
}
The Question
It looks so ugly, Is there more neat and shorter way of doing this? using Linq maybe?
This is one that works and takes care of some edge cases also:
var realDates = splitDates
.Where(d => d > dateFrom && d < dateTo)
.Concat(new List<DateTime>() {dateFrom.AddDays(-1), dateTo})
.Select(d => d.Date)
.Distinct()
.OrderBy(d => d)
.ToList();
// now we have (start - 1) -- split1 -- split2 -- split3 -- end
// we zip it against split1 -- split2 -- split3 -- end
// and produce start,split1 -- split1+1,split2 -- split2+1,split3 -- split3+1,end
realDates.Zip(realDates.Skip(1), (a, b) => Tuple.Create(a.AddDays(1), b));
You can do it like this:
List<DateTime> split =
splitDates.Where(d => d >= dateFrom && d <= dateTo).ToList();
List<Tuple<DateTime, DateTime>> periods =
Enumerable.Range(0, split.Count + 1)
.Select(i => new Tuple<DateTime, DateTime>(
i == 0 ? dateFrom : split[i - 1].AddDays(1),
i == split.Count ? dateTo : split[i]
))
.ToList();
While L.B is correct and this probably belongs on Code Review, I felt like taking a crack at this:
Given Your First Code Block, the following code will do what you're asking for:
// List of all dates in order that are valid
var dateSegments = new [] { dateFrom, dateTo }
.Concat(splitDates.Where(x => x > dateFrom && x < dateTo))
.OrderBy(x => x)
.ToArray();
List<Tuple<DateTime, DateTime>> results = new List<Tuple<DateTime, DateTime>>();
for(var i = 0; i < dateSegments.Length - 1; i++)
{
results.Add(new Tuple<DateTime, DateTime>(dateSegments[i], dateSegments[i+1]));
}
If you put all the dates into a single list, then this should work:
var dates = new List<DateTime>
{
new DateTime(2012, 1, 1),
new DateTime(2012, 2, 1),
new DateTime(2012, 5, 1),
new DateTime(2012, 7, 1),
new DateTime(2012, 11, 1),
new DateTime(2012, 12, 31)
};
var z = dates.Zip(dates.Skip(1), (f, s) => Tuple.Create(f.Equals(dates[0]) ? f : f.AddDays(1), s));
List<DateTime> splitDates = GetSplitDates();
DateTime dateFrom = GetDateFrom();
DateTime dateTo = GetDateTo();
List<DateTime> edges = splitDates
.Where(d => dateFrom < d && d < dateTo)
.Concat(new List<DateTime>() {dateFrom, dateTo})
.Distinct()
.OrderBy(d => d)
.ToList();
//must be at least one edge since we added at least one unique date to this.
DateTime currentEdge = edges.First();
List<Tuple<DateTime, DateTime>> resultItems = new List<Tuple<DateTime, DateTime>>();
foreach(DateTime nextEdge in edges.Skip(1))
{
resultItems.Add(Tuple.Create(currentEdge, nextEdge));
currentEdge = nextEdge;
}
return resultItems;
I have Made it simple to get the Dates between the DateRange provided.
Model Object
public class DateObjectClass
{
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
}
Action :
public List<DateObjectClass> SplitDateRangeByDates(DateTime start,DateTime end)
{
List<DateObjectClass> datesCollection = new List<DateObjectClass>();
DateTime startOfThisPeriod = start;
while (startOfThisPeriod < end)
{
DateTime endOfThisPeriod =new DateTime(startOfThisPeriod.Year,startOfThisPeriod.Month,startOfThisPeriod.Day,23,59,59);
endOfThisPeriod = endOfThisPeriod < end ? endOfThisPeriod : end;
datesCollection.Add(new DateObjectClass() { startDate= startOfThisPeriod ,endDate =endOfThisPeriod});
startOfThisPeriod = endOfThisPeriod;
startOfThisPeriod = startOfThisPeriod.AddSeconds(1);
}
return datesCollection;
}

Categories