views:

67

answers:

2

Hi

I am tring to get a list of dates from my db that will eventually be used to populate a calendar. Each 'calendar event' has a start date & end date, i need to get all dates between & including the start & end date.

i am stuck on the WHERE statement, as i am not sure what to use for this

public List<EventFeed> GetCalendarDates()
    {

        return (from eventsList in GetEventsList()      
                select new EventFeed()
                        {
                          //EventDate = todo
                        }).ToList();
    }

UPDATE

just to be clear, if i have a calendar event called foobar which starts on 22/08/2010 and ends on 24/08/2010, then i want my list return:

22/08/2010, 23/08/2010, 24/08/2010

thanks kb

A: 

You mean you want to select all the events that started on or after start date and ended on or before end date!

If yes, then this will help

var query = from @event in events
                        where @event.Start.Date >= startDate.Date
                              && @event.End.Date <= endDate.Date
                        select @event;
mho
Hi mho, thanks for your reply, but where does this come [email protected]
kb
Hi mho, i have updated my post...
kb
+1  A: 

I had to do something similar recently, I used a Func<> to extract the dates from the range and used the result in the linq query.

I have added the same Func to your Linq query below. You didn't specify the name of the object that is returned by GetEventsList() so just replace the EventItem type for the first type parameter in the Func<> with whatever type you need.

public static List<EventFeed> GetCalendarDates()
{
    Func<EventItem, List<DateTime>> extractEventDates = eventItem =>
                                                          {
                                                              var dates = new List<DateTime>();
                                                              for (var date = eventItem.StartDate;
                                                                   date <= eventItem.EndDate;
                                                                   date = date.AddDays(1))
                                                              {
                                                                  dates.Add(date);
                                                              }
                                                              return dates;
                                                          };

    return (from eventItem in GetEventsList()
            from eventDate in extractEventDates(eventItem)
            select new EventFeed
                       {
                           EventDate = eventDate
                       }).ToList();
}
fletcher
thanks fletcher, i came up with something similiar, but your solution is less code, thanks kb
kb