使用 rx.net 工作类型表文档类型表JobTypeDocumentType 包含以下列:

问题描述

在我的数据库中,我有一个连接表来存储 JobTypeDocumentType(简化描述)之间的多对多关系

工作类型表

类型
身份证 内部
说明 varchar(100)

文档类型表

类型
身份证 内部
说明 varchar(100)

JobTypeDocumentType 包含以下列:

类型
身份证 内部
工作类型 内部
文档类型 内部

我想从数据库中检索数据并将结果记录集转换为 IObservable<collection<viewmodels>>

在下面的代码中,GetJobDocumentTypes 方法返回 JobTypeDocumentType POCO 的集合:

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;
        }

但是,我坚持使用 IObservable<IEnumerable<IObservable<JobTypeDocumentTypeviewmodel>>> 类型的结果

任何帮助和/或建议将不胜感激。

解决方法

在这种情况下,您必须使用 SelectMany

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...