Snapper
Snapper

Reputation: 686

linq select where between dates

I'm trying to add to the dates all the events between Date and DateFinal in order to fill the calendar with the events.

I've already searched but I can't find any solution for this.

pageItems.Add("dates", allEvents.Select(i => i.Date).ToList());

This is what I have so far but only show the days of i.Date and I want to show all of the days between Date and DateFinal.

Cheers and thanks in advance

In the allEvents I have

allEvents = Current.Descendants(n => n.NodeTypeAlias == "EventPage")
            .get_Items()
            .Select(n => new{
            Node = n,
            Date = (Helper.ParseXmlDate(n.GetProperty("itemDate")) ?? n.UpdateDate).DatePart(),
            DateFinal = (Helper.ParseXmlDate(n.GetProperty("itemDateFinal")) ?? n.UpdateDate).DatePart()
                });

Upvotes: 2

Views: 19525

Answers (3)

sa_ddam213
sa_ddam213

Reputation: 43596

First.. Sorry if I have misunderstood the question

If you have 2 DateTime and you want to select a list of all the Days(as DateTime) between those 2 dates, you could use Enumerable.Range using the amount of days between the Date and DateFinal to loop in your select statement to add a day to the start date and output a list of DateTimes

This will select all the dates between Date and DateFinal.

  allevents.Select(i => Enumerable.Range(1, (i.DateFinal - i.Date).Days).Select(dayCount => i.Date.AddDays(dayCount))).ToList()

If you need to include Date and DateFinal to the list you can use

  allevents.Select(i => Enumerable.Range(0, 1 + (i.DateFinal - i.Date).Days).Select(dayCount => i.Date.AddDays(dayCount))).ToList()

Input:

Date: 02/20/2013
DateFinal: 02/31/2013

OutPut:

02/20/2013
02/21/2013
02/22/2013
02/23/2013
...

Is that what you mean?

Upvotes: 3

YD1m
YD1m

Reputation: 5895

Use this:

allEvents.Where(i => i.Date > Date && i.Date < DateFinal).Select(i => i.Date).ToList()

Upvotes: 5

Tigran
Tigran

Reputation: 62248

You probably searching for:

TimeSpan span=d2-d1;
span.TotalDays;

so it should look like:

allEvents.Select(i => (DateFinal - i.Date).TotalDays).ToList()

This shows all days between some DateFinal and i.Date

If this is not what you're searching for, please clarify.

Upvotes: 2

Related Questions