Q:

c# - Drop Down List of Date Ranges to Filter Relevant Data

I have to create a drop down that will allow the user to select a date range, and given their selection, display all records with dates between the range. 
I know how to query one range (code snippet below), but how do I query such that the drop down only populates with the options of "Last 7 Days", "Last 15 Days", "Last 30 Days", "Last 60 Days", and "All Transactions" (and then displays the corresponding transactions based on selection)?
Also would be nice to know how to have an option called "Custom" that allows the user to input their own date range, but that is not a priority.
What I have below will query and display transactions within the last seven days, but this obviously is not achieving what I am looking for.
public SelectList GetDateRanges()
    {
        DateTime today = DateTime.Today;
        DateTime minusSeven = today.AddDays(-7);
        DateTime minusFifteen = today.AddDays(-15);
        DateTime minusThirty = today.AddDays(-30);
        DateTime minusSixty = today.AddDays(-60);

        var query = from t in db.Transactions
                    where t.TransactionDate >= minusSeven
                    select t;

My thought is that I possibly could create a static list, but then I don't know how I would query based on a user's selection within that list.

A:

Pass the period back to your function
public SelectList GetDateRanges(int daysBack)
{
    var oldestDate = (DateTime.Today).AddDays(daysBack * -1);

    var query = from t in db.Transactions
                where t.TransactionDate >= oldestDate
                select t;
    ...
}

And pre-set the daysBack values in the select control.
<select name="daysBack">
    <option value="7">Last 7 days</option>
    <option value="15">Last 15 days</option>
</select>

If you ultimately want to give the user full control then pass the two date boundaries. Every other option is just a subset of this query.
public SelectList GetDateRanges(DateTime startDate, DateTime endDate)
{
    var query = from t in db.Transactions
                where t.TransactionDate >= startDate &&
                      t.TransactionDate <= endDate
                select t;
    ...
}

Then the you can use a client-side plug-in to do the work of providing the two dates.

