Linq query to get all values from between two dates - c#

Is it possible to use linq to create a query to get a range of values from between two dates used as delimiters?
ex.
Get all entities that have a value of Date column between 12/12/12 and 12/12/13.
I'm only looking for a hint or an approach.

Of course you can, there is no "between" syntax if that's what you are getting at but it's relatively simple to do e.g.
list.Where(x => x.Date >= StartDate && x.Date <= EndDate);
If you want better readability, you could even write an extension method for DateTime i.e.
public static class DateTimeHelpers
{
public static bool Between(this DateTime dateTime, DateTime startDate, DateTime endDate)
{
return dateTime >= startDate && dateTime <= endDate;
}
}
...
list.Where(x => x.Date.Between(StartDate, EndDate));

public IEnumerable<DateTime> test(DateTime dt, DateTime dt2)
{
// check if dt is smaller (or the same) as dt2 and code this out or throw error
// replace dates with actual class / code
List<DateTime> dates = new List<DateTime>();
return dates.Where(d => d >= dt && d <= dt2);
}

var stuffBetweenDates = ListOfStuff.Where(c => c.Date >= 12/12/12 && c.Date <= 12/12/13);
Something along those lines I think. Obviously where I have put 12/12/12 and 12/12/13 you need to put recognized Date Types in those places.

Related

How to get dates between two dates in C#

I would like to get dates between two dates. Instead of expected 9 different dates, I get 875287 and run out of memory. What would be the problem with the code below?
StartDate value is 01/04/2016 00:00:00
EndDate value is 10/04/2016 00:00:00
var selectedDates = new List<DateTime?>();
for (var date = StartDate; date <= EndDate; date.Value.AddDays(1))
{
selectedDates.Add(date);
}
You aren't assigning the value of date.Value.AddDays(1) to anything, so it ends up in an infinite loop. You'd need to change your code so that date is set to the result of AddDays.
for (var date = StartDate; date <= EndDate; date = date.AddDays(1))
{
selectedDates.Add(date);
}
LINQ solution (let's generate selectedDates):
var selectedDates = Enumerable
.Range(0, int.MaxValue)
.Select(index => new DateTime?(StartDate.AddDays(index)))
.TakeWhile(date => date <= EndDate)
.ToList();
As far as I can see, since AddDays method returns a new instance of a DateTime, it does not change the current instance since DateTime is immutable.
Looks like your date is DateTime?, you can change this part as;
for (var date = StartDate; date <= EndDate; date = date.Value.AddDays(1))
{
selectedDates.Add(date);
}
As usr pointed, this might be affected on DST. You might wanna check Dmitry's answer as well.
A shorter notation using Linq's Range method uses the ability to already figure out the number of days using the TimeSpan.Days property after subtracting start from end.
Assuming the start is before end you'd end up with:
DateTime StartDate = new DateTime(1979, 10, 4);
DateTime EndDate = new DateTime(2016, 10, 4);
var dates = Enumerable.Range(0, (EndDate - StartDate).Days + 1)
.Select(day => StartDate.AddDays(day))
If you need it to be Nullable, add:
.Cast<DateTime?>()
If you need this to be a List, add:
.ToList()
It's probably quite a bit more efficient than the other LINQ based solution.
Decided to change it up with a do/while
var selectedDates = new List<DateTime?>();
DateTime? StartDate = DateTime.Parse("01/04/2016 00:00:00");
DateTime? EndDate = DateTime.Parse("10/04/2016 00:00:00");
do
{
selectedDates.Add(StartDate);
StartDate = StartDate.Value.AddDays(1);
}while(StartDate < EndDate);

Array find all dates before current date

Hello I am currently trying to array.find all the dates before the current date. I have tried both using the datetime.now as well as creating a seperate variable for current date in my struct but i keep getting "Cannot implicitly convert type 'Assignment_3.Program.Hire' to 'System.DateTime". I'm sure the solution is quite simple but as a novice it does escape from me. If you need any more of the code I will be happy to provide
struct Hire
{
public int CustomerNo;
public int DVDNo;
public DateTime HireDate;
public int NoNights;
public DateTime CurrentDate = DateTime.Now;
}
DateTime result = Array.Find(hiredetails, Hire => Hire.HireDate <= Hire.CurrentDate);
Array.Find<T> returns the element matching the criteria. In you case since it is an array of Hire type, it will return element of type Hire, which you cannot assign to DateTime. You can do:
List<DateTime> allDates = hiredetails.Where(hire=> hire.HireDate <= hire.CurrentDate)
.Select(r=> r.HireDate)
.ToList();
You can also return IEnumerable<DateTime> and exclude ToList() from the above statement.
Not really sure if you need this but instead of keeping the current date inside the object you can have that in your local variable and pass that in your query like:
DateTime currentDate = DateTime.Now;
List<DateTime> allDates = hiredetails.Where(hire=> hire.HireDate <= currentDate)
.Select(r=> r.HireDate)
.ToList();
Do not store current date in a structure, use local variable instead, solution would look like:
var currentDate = DateTime.Now;
var result = hiredetails.Select(h => h.HireDate).Where(d => d <= currentDate);

Iterate between two dates in C#

I have two dates:
DateTime fromDate = new DateTime(2013,7,27,12,0,0);
DateTime toDate = new DateTime(2013,7,30,12,0,0);
I want to iterate from fromDate to toDate by incrementing fromDate with a single day and the loop should break when fromDate becomes equal to or greater than the toDate. I have tried this:
while(fromDate < toDate)
{
fromDate.AddDays(1);
}
But this is an infinite loop and won't stop. How can I do this ?
Untested but should work:
for(DateTime date = fromDate; date < toDate; date = date.AddDays(1)) {
}
Modify the comparison to <= if you want to include toDate as well.
DateTime.AddDays indeed adds the specified number of days to the date - but the resulting date is returned as a new DateTime value; the original DateTime value is not changed.
Therefore, make sure you assign the result of the your operation back to the variable you inspect in your loop condition:
while (fromDate < toDate)
{
fromDate = fromDate.AddDays(1);
}

Get list of dates from StartDate to EndDate

How can i get list of dates between start and end dates using linq or lambda expression.
DateTime StartDate = DateTime.Now;
DateTime EndDate = DateTime.Now.AddMonths(13);
List<DateTime> ListofDates = //Contains list of all dates only between start and end date.
Enumerable.Range(0, (EndDate - StartDate).Days).Select(i => StartDate.AddDays(i))
First of all, when you want Dates and not Datetimes you better use DateTime.Now.Date instead of just DateTime.Now
I've edited SLaks code so you can get the list of dates you want.
DateTime StartDate = DateTime.Now.Date;
DateTime EndDate = DateTime.Now.Date.AddMonths(13);
IEnumerable<double> daysToAdd = Enumerable.Range(0,
(EndDate - StartDate).Days + 1)
.ToList().ConvertAll(d => (double)d);
IEnumerable<DateTime> ListOfDates = daysToAdd.Select(StartDate.AddDays).ToList();
Note that I've added +1 in the enumerable so you can have both the start and end date (you didn't specify it)
List<DateTime> ListofDates = Enumerable.Range(0, (EndDate - StartDate).Days)
.Select(i => StartDate.AddDays(i))
.ToList();

Iterate over each Day between StartDate and EndDate [duplicate]

This question already has answers here:
How do I loop through a date range?
(17 answers)
Closed 6 years ago.
I have a DateTime StartDate and EndDate.
How can I, irrespective of times, iterate across each Day between those two?
Example: StartDate is 7/20/2010 5:10:32 PM and EndDate is 7/29/2010
1:59:12 AM.
I want to be able to iterate across 7/20, 7/21, 7/22 .. 7/29.
for(DateTime date = StartDate; date.Date <= EndDate.Date; date = date.AddDays(1))
{
...
}
The .Date is to make sure you have that last day, like in the example.
An alternative method that might be more reusable is to write an extension method on DateTime and return an IEnumerable.
For example, you can define a class:
public static class MyExtensions
{
public static IEnumerable EachDay(this DateTime start, DateTime end)
{
// Remove time info from start date (we only care about day).
DateTime currentDay = new DateTime(start.Year, start.Month, start.Day);
while (currentDay <= end)
{
yield return currentDay;
currentDay = currentDay.AddDays(1);
}
}
}
Now in the calling code you can do the following:
DateTime start = DateTime.Now;
DateTime end = start.AddDays(20);
foreach (var day in start.EachDay(end))
{
...
}
Another advantage to this approach is that it makes it trivial to add EachWeek, EachMonth etc. These will then all be accessible on DateTime.
You have to be careful about end-date. For example, in
Example: StartDate is 7/20/2010 5:10:32 PM and EndDate is 7/29/2010 1:59:12 AM.
I want to be able to iterate across 7/20, 7/21, 7/22 .. 7/29.
date < endDate will not include 7/29 ever. When you add 1 day to 7/28 5:10 PM - it becomes 7/29 5:10 PM which is higher than 7/29 2 AM.
If that is not what you want then I'd say you do
for (DateTime date = start.Date; date <= end.Date; date += TimeSpan.FromDays(1))
{
Console.WriteLine(date.ToString());
}
or something to that effect.
The loops of #Yuriy Faktorovich, #healsjnr and #mho will all throw a System.ArgumentOutOfRangeException: The added or subtracted value results in an un-representable DateTime
exception if EndDate == DateTime.MaxValue.
To prevent this, add an extra check at the end of the loop
for(DateTime date = StartDate; date.Date <= EndDate.Date; date = date.AddDays(1))
{
...
if (date.Date == DateTime.MaxValue.Date)
{
break;
}
}
(I would have posted this as a comment to #Yuriy Faktorovich's answer, but I lack reputation)
DateTime date = DateTime.Now;
DateTime endDate = date.AddDays(10);
while (date < endDate)
{
Console.WriteLine(date);
date = date.AddDays(1);
}

Categories