问题描述
在我的数据库中,我有一个连接表来存储 JobType
和 DocumentType
(简化描述)之间的多对多关系
工作类型表
列 | 类型 |
---|---|
身份证 | 内部 |
说明 | 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)))
);