convert join table using rx.net

66 Views Asked by At

In my database I have a join table to store many to many relationships between a JobType and DocumentType (simplified description)

JobType Table

Column Type
Id int
Description varchar(100)

DocumentType Table

Column Type
Id int
Description varchar(100)

JobTypeDocumentType with the following columns:

Column Type
Id int
JobType int
DocumentType int

I want to retrieve the data from the database and convert the result recordset into an IObservable<collection<ViewModels>>.

In the code below, the GetJobDocumentTypes method returns a collection of JobTypeDocumentType POCOs:

IObservable<IReadOnlyCollection<JobTypeDocumentType>> dataService.GetJobDocumentTypes(int jobTypeId)` 

The purpose of getJobTypeDocumenTypeViewModels is to transform the returned POCOs into ViewModels:



private IObservable<IEnumerable<JobTypeDocumentTypeViewModel>> getJobTypeDocumenTypeViewModels(JobType jobType)
        {
            var result = dataService.GetJobDocumentTypes(jobType.Id)
                .Select(items => items.Select(jtdt => 
                        dataService.GetById<DocumentType>(jtdt.DocumentType_Id)
                                   .Select(dt => new JobTypeDocumentTypeViewModel(jtdt, jobType, dt))));
            return result;
        }

However, I am stuck with result being of type IObservable<IEnumerable<IObservable<JobTypeDocumentTypeViewModel>>>

Any help and/or suggestions would be appreciated.

1

There are 1 best solutions below

3
On

You have to use SelectMany in this case:

var result = dataService.GetJobDocumentTypes(jobType.Id)
    .SelectMany(items => items
        .Select(jtdt => dataService.GetById<DocumentType>(jtdt.DocumentType_Id)
        .Select(dt => new JobTypeDocumentTypeViewModel(jtdt, jobType, dt)))
    );