protected override async Task <IReadOnlyCollection <AuditRecord <Guid> > > Process(SessionInstructorHistoryQuery request, CancellationToken cancellationToken)
        {
            var parameters = new List <object>();

            var sql = new StringBuilder();

            sql.AppendLine("SELECT S.*");
            sql.AppendLine("FROM [IQ].[SessionInstructor]");
            sql.AppendLine("FOR SYSTEM_TIME ALL AS S");

            if (request.SessionId.HasValue)
            {
                sql.AppendLine("WHERE S.[SessionId] = {0}");
                parameters.Add(request.SessionId.Value);
            }
            else if (request.TopicId.HasValue)
            {
                sql.AppendLine("INNER JOIN [IQ].[Session] AS J ON J.[Id] = S.[SessionId]");
                sql.AppendLine("WHERE J.[TopicId] = {0}");
                parameters.Add(request.TopicId.Value);
            }

            var entities = await DataContext.SessionInstructors
                           .FromSqlRaw(sql.ToString(), parameters.ToArray())
                           .AsNoTracking()
                           .OrderBy(p => p.PeriodEnd)
                           .ProjectTo <SessionInstructorModel>(Mapper.ConfigurationProvider)
                           .ToListAsync(cancellationToken);

            var historyList = new List <AuditRecord <Guid> >();

            // group changes by id
            foreach (var group in entities.GroupBy(p => p.Id))
            {
                var groupList = group
                                .OrderBy(p => p.PeriodEnd)
                                .ToList();

                var changeList = _changeCollector.CollectChanges(groupList, nameof(SessionInstructor), s => s.DisplayName);
                historyList.AddRange(changeList);
            }

            return(historyList.ToList());
        }
        private async Task <List <AuditRecord <Guid> > > CollectTopicHistory(TopicHistoryQuery request, CancellationToken cancellationToken)
        {
            var sql = new StringBuilder();

            sql.AppendLine("SELECT *");
            sql.AppendLine("FROM [IQ].[Topic]");
            sql.AppendLine("FOR SYSTEM_TIME ALL");
            sql.AppendLine("WHERE [Id] = {0}");

            var entities = await DataContext.Topics
                           .FromSqlRaw(sql.ToString(), request.Id)
                           .AsNoTracking()
                           .OrderBy(p => p.PeriodEnd)
                           .ProjectTo <TopicReadModel>(Mapper.ConfigurationProvider)
                           .ToListAsync(cancellationToken);

            var historyList = _changeCollector.CollectChanges(entities, nameof(Topic), t => t.Title);

            return(historyList.ToList());
        }