Code duplication where impliment extension method for IEnumerable and IQueryable on entity set Since Entity Set does not support asqueryable, I have to do this.  How can I reduce the amount of redundant code between the two methods?

public static IQueryable&lt;T&gt; WhereCreateWithin&lt;T&gt;(this IQueryable&lt;T&gt; query, DateManager.TimeRange timeRange, DateTime? beginDayParam = null) where T : IHasCreateDate
{
    if (beginDayParam == null)
    {
        beginDayParam = DateTime.Today;
    }

    DateTime beginDay = beginDayParam.Value.Date; 
    DateTime startRange;
    DateTime endRange;

    switch (timeRange)
    {
        case DateManager.TimeRange.All:

            return query;

        case DateManager.TimeRange.ThisYear:

            startRange = new DateTime(beginDay.Year, 1, 1); // January 1, this year
            endRange = startRange.AddYears(1);

            return query.Where(p =&gt; p.create_date.Date &gt;= startRange &amp;&amp; p.create_date &lt; endRange);

        case DateManager.TimeRange.ThisMonth:

            startRange = new DateTime(beginDay.Year, beginDay.Month, 1);
            endRange = startRange.AddMonths(1).Date;

            return query.Where(p =&gt; p.create_date.Date &gt;= startRange &amp;&amp; p.create_date &lt; endRange);

        case DateManager.TimeRange.ThisWeek:

            startRange = BuilkHelper.DateHelper.StartOfWeek(beginDay, DayOfWeek.Monday);
            endRange = startRange.AddDays(6);

            return query.Where(p =&gt; p.create_date.Date &gt;= startRange &amp;&amp; p.create_date &lt; endRange);

        case DateManager.TimeRange.ToDay:

            return query.Where(p =&gt; p.create_date.Date == beginDay);

        case DateManager.TimeRange.Last30Day:
            startRange = beginDay.AddDays(-29);
            endRange = beginDay;

            return query.Where(p =&gt; p.create_date.Date &gt;= startRange &amp;&amp; p.create_date &lt;= endRange);
    }
    return query;
}

public static IEnumerable&lt;T&gt; WhereCreateWithin&lt;T&gt;(this IEnumerable&lt;T&gt; query, DateManager.TimeRange timeRange, DateTime? beginDayParam = null) where T : IHasCreateDate
{
    if (beginDayParam == null)
    {
        beginDayParam = DateTime.Today;
    }

    DateTime beginDay = beginDayParam.Value.Date;
    DateTime startRange;
    DateTime endRange;

    switch (timeRange)
    {
        case DateManager.TimeRange.All:

            return query;

        case DateManager.TimeRange.ThisYear:

            startRange = new DateTime(beginDay.Year, 1, 1); // January 1, this year
            endRange = startRange.AddYears(1);

            return query.Where(p =&gt; p.create_date.Date &gt;= startRange &amp;&amp; p.create_date &lt; endRange);

        case DateManager.TimeRange.ThisMonth:

            startRange = new DateTime(beginDay.Year, beginDay.Month, 1);
            endRange = startRange.AddMonths(1).Date;

            return query.Where(p =&gt; p.create_date.Date &gt;= startRange &amp;&amp; p.create_date &lt; endRange);

        case DateManager.TimeRange.ThisWeek:

            startRange = BuilkHelper.DateHelper.StartOfWeek(beginDay, DayOfWeek.Monday);
            endRange = startRange.AddDays(6);

            return query.Where(p =&gt; p.create_date.Date &gt;= startRange &amp;&amp; p.create_date &lt; endRange);

        case DateManager.TimeRange.ToDay:

            return query.Where(p =&gt; p.create_date.Date == beginDay);

        case DateManager.TimeRange.Last30Day:
            startRange = beginDay.AddDays(-29);
            endRange = beginDay;

            return query.Where(p =&gt; p.create_date.Date &gt;= startRange &amp;&amp; p.create_date &lt;= endRange);
    }
    return query;
}
Given that the logic is exactly the same (i did not do a line by line comparision), one of the possible solutions whould be:

private static TCollection WhereCreateWithin&lt;T, TCollection&gt;(TCollection query,
             DateManager.TimeRange timeRange, 
             DateTime? beginDayParam,
             Func&lt;TCollection, Func&lt;T, bool&gt;, TCollection&gt; selector) 
    where T : IHasCreateDate
    where TCollection : IEnumerable&lt;T&gt;
{
    //insert your logic here
    //replace query.Where(x =&gt; ... ) call with selector(query, x =&gt; ... ) call
}

public static public static IQueryable&lt;T&gt; WhereCreateWithin&lt;T&gt;(this IQueryable&lt;T&gt; query, DateManager.TimeRange timeRange, DateTime? beginDayParam = null) where T : IHasCreateDate
{
    return WhereCreateWithin&lt;T, IQueryable&lt;T&gt;&gt;(query, timeRange, beginDayParam, (q, f) =&gt; q.Where(x =&gt; f(x)))
}


public static public static IEnumerable&lt;T&gt; WhereCreateWithin&lt;T&gt;(this IEnumerable&lt;T&gt; query, DateManager.TimeRange timeRange, DateTime? beginDayParam = null) where T : IHasCreateDate
{
    return WhereCreateWithin&lt;T, IEnumerable&lt;T&gt;&gt;(query, timeRange, beginDayParam, (q, f) =&gt; q.Where(f))
}