Is there any way to make this query faster?
I'm trying to retrieve a list of schedules that occur between a start and end date. I have a parent record and list of child records for every schedule.

    Schedule
      Id

    ScheduleDates
      ScheduleId
      Start
      End

The end goal is to be able to show a grouped list of dates. So if I have multiple schedules that start on the same day and end on the same day, they should be grouped together.

    Jan 10 - 15
      - Schedule 1
      - Schedule 2
    Jan 10 - 20
      - Schedule 3
      - Schedule 4
    Feb 1 - 3
      - Schedule 5

The first query looks something like this:

    var schedules = from schedule in _db.Schedules
                    join date in _db.ScheduleDates on schedule.Id equals date.ScheduleId
                    // join a bunch of other tables
                    // bunch of where queries
                    && date.Start >= startDate
                    && date.Start <= endDate
                    select schedule;

This returns the records I want, my problem is when I need to group the records together by the earliest and latest child records. The rest don't matter.

Trying to add the following works

    group new {
      //
    } by new { 
       Start = schedule.ScheduleDates.Min(x=>x.Start),
       End = schedule.ScheduleDates.Max(x=>x.Start)
    }

It just doesn't work well. When I have several thousand records it is incredibly slow because it's basically executing a query with the following where clause for every single row:

    WHERE (((@x1 IS NULL) AND (((
        SELECT MIN([t4].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t4]
        WHERE [t4].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )) IS NULL)) OR ((@x1 IS NOT NULL) AND (((
        SELECT MIN([t5].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t5]
        WHERE [t5].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )) IS NOT NULL) AND (((@x1 IS NULL) AND (((
        SELECT MIN([t6].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t6]
        WHERE [t6].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )) IS NULL)) OR ((@x1 IS NOT NULL) AND (((
        SELECT MIN([t7].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t7]
        WHERE [t7].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )) IS NOT NULL) AND (@x1 = ((
        SELECT MIN([t8].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t8]
        WHERE [t8].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        ))))))) AND (((@x2 IS NULL) AND (((
        SELECT MAX([t9].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t9]
        WHERE [t9].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )) IS NULL)) OR ((@x2 IS NOT NULL) AND (((
        SELECT MAX([t10].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t10]
        WHERE [t10].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )) IS NOT NULL) AND (((@x2 IS NULL) AND (((
        SELECT MAX([t11].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t11]
        WHERE [t11].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )) IS NULL)) OR ((@x2 IS NOT NULL) AND (((
        SELECT MAX([t12].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t12]
        WHERE [t12].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )) IS NOT NULL) AND (@x2 = ((
        SELECT MAX([t13].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t13]
        WHERE [t13].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )))))))

Is there anything I can do about this?
Is there any way to make this query faster?
===

I'm trying to retrieve a list of schedules that occur between a start and end date. I have a parent record and list of child records for every schedule.

    Schedule
      Id

    ScheduleDates
      ScheduleId
      Start
      End

The end goal is to be able to show a grouped list of dates. So if I have multiple schedules that start on the same day and end on the same day, they should be grouped together.

    Jan 10 - 15
      - Schedule 1
      - Schedule 2
    Jan 10 - 20
      - Schedule 3
      - Schedule 4
    Feb 1 - 3
      - Schedule 5

The first query looks something like this:

    var schedules = from schedule in _db.Schedules
                    join date in _db.ScheduleDates on schedule.Id equals date.ScheduleId
                    // join a bunch of other tables
                    // bunch of where queries
                    && date.Start >= startDate
                    && date.Start <= endDate
                    select schedule;

This returns the records I want, my problem is when I need to group the records together by the earliest and latest child records. The rest don't matter.

Trying to add the following works

    group new {
      //
    } by new { 
       Start = schedule.ScheduleDates.Min(x=>x.Start),
       End = schedule.ScheduleDates.Max(x=>x.Start)
    }

It just doesn't work well. When I have several thousand records it is incredibly slow because it's basically executing a query with the following where clause for every single row:

    WHERE (((@x1 IS NULL) AND (((
        SELECT MIN([t4].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t4]
        WHERE [t4].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )) IS NULL)) OR ((@x1 IS NOT NULL) AND (((
        SELECT MIN([t5].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t5]
        WHERE [t5].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )) IS NOT NULL) AND (((@x1 IS NULL) AND (((
        SELECT MIN([t6].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t6]
        WHERE [t6].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )) IS NULL)) OR ((@x1 IS NOT NULL) AND (((
        SELECT MIN([t7].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t7]
        WHERE [t7].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )) IS NOT NULL) AND (@x1 = ((
        SELECT MIN([t8].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t8]
        WHERE [t8].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        ))))))) AND (((@x2 IS NULL) AND (((
        SELECT MAX([t9].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t9]
        WHERE [t9].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )) IS NULL)) OR ((@x2 IS NOT NULL) AND (((
        SELECT MAX([t10].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t10]
        WHERE [t10].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )) IS NOT NULL) AND (((@x2 IS NULL) AND (((
        SELECT MAX([t11].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t11]
        WHERE [t11].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )) IS NULL)) OR ((@x2 IS NOT NULL) AND (((
        SELECT MAX([t12].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t12]
        WHERE [t12].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )) IS NOT NULL) AND (@x2 = ((
        SELECT MAX([t13].[StartDate])
        FROM [dbo].[ScheduleDate] AS [t13]
        WHERE [t13].[ScheduleDate_ScheduleId] = [t1].[ScheduleId]
        )))))))

Is there anything I can do about this?