Click here to Skip to main content
15,913,854 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Is there not some LINQ magic that I could use to generate an IEnumerable comprising one DateTime for each day between two dates? Currently I'm using a loop, but it just seems clumsy.
Posted

No, there's no magical LINQ method to call that I'm aware of. The Enumerable.Range() method only uses integers.

I'm pretty sure that the Range() method just uses a loop with yield return underneath the sheets. I don't believe there's any other way around it. As such, something as simple as this should suffice. You can add similar methods to create ranges by whatever unit of time you want.

C#
public class DateTimeEnumerable
{
    public static IEnumerable<DateTime> RangeDays(DateTime startDate, DateTime endDate)
    {
        DateTime currentDate = startDate;
        while (currentDate < endDate)
        {
            currentDate = currentDate.AddDays(1);
            yield return currentDate;
        }
    }
}
 
Share this answer
 
v2
IMHO LINQ is for querying data or creating one enumerable from an existing collection. Hence I believe LINQ should not be used here.

BK: Ah, but I want to query one collection, with two values in it, and receive a collection as a response, one with the original two values and all integral values between them.
 
Share this answer
 
v2
Again, you are trying to "create" something through LINQ which, IMHO, is not the purpose why LINQ exists.

Only thing which I know about creating values through LINQ is Range method which already is told in the other post.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900