I have an events table with a ‘featureEvent’ boolean column.
I need to display a featured event of the day on the home page
what id like to do is display the featured event for the entire day. For this i think i need to link it to the date somehow.
public Event GetTodaysFeatureEvent()
{
var ev = db.Events.Where(x => x.featureEvent == true && x.eventDate> DateTime.Now.Date);
//gets a pool of feature-able events
}
how can I do it so that whatever event is displayed is the same one for the entire day?
Edit – I’ll try making more sense.
I set events to be featureable (featureEvent=true). what i want to do is pick out from a pool of featurable events (that will occur in the future) one single event.
i dont want to use firstOrDefault as this could potentially show the same event N days in a row. ideally I’d like something whereby a different event is selected from the featurable events each day… i cant just pick a random one as during the same day different events will be featured and for different users. so i want that one event to be shown the entire day to all users.
I dont mind featuring the same event more than once, ie if all the featurable events have already been featured then go back to the first one?
now it gets even more complicated as i can set an event to featured at any time so not sure how to handle that side of things.
hope thats a little clearer….probably not!
(This answer assumes that
eventDatealways has a time component of zero — in other words, that the value would always be something like12/31/2012 00:00:00.000and never something like12/31/2012 11:30:00.000.)Note that your logic only chooses events occurring after today. To choose events occurring today or later, change
>to>=. To choose events occurring today only, use==.If it is possible to have more than one feature event for a given day, you can do this in one of two ways
pick an arbitrary item, and save a reference to it somewhere so you can reuse it, or
apply some unambiguous ordering to your linq query, so that the objects are guaranteed always to have the same order; then pick the first one.
For example, assuming there’s a property
Pwhose value is unique to each event, and whose type isTwhere T implementsIComparable<T>, then you could do this:This picks a future event that occurs on the next day for which there’s at least one event. Since the events are ordered by
P, andPis by assumption unique, the events will always be retrieved in the same order.If you prefer to display featured events only on the actual day (and display nothing on days that have no featured events), then change the
>=to==:In response to your edits:
To achieve this, you’ll definitely need persistent information about which event is featured on a given day. It seems now that your question is more about how to do that than about specific linq expressions.
If you want to make sure that events aren’t featured repeatedly before all other events have been featured, you could save a “lastFeaturedOn” date for each featured event. When choosing an event to feature, first pick one that was never featured (i.e., lastFeaturedOn == null); if there are no such events, pick the one with the smallest lastFeaturedOn date.
This will give you the events in the same repeated order; if you’d rather reshuffle them you could instead set all dates to null when they are all non-null (or use a boolean).
Once you’ve picked an event, you’ll need to save it and a date in your database, to identify it as the featured event for that date. You could use the “lastFeaturedOn” date, and define “today’s featured item” as “the item whose lastFeaturedOn date is today’s date.”