【问题标题】:convert join table using rx.net使用 rx.net 转换连接表
【发布时间】:2021-04-26 21:45:37
【问题描述】:

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

工作类型表

Column Type
Id int
Description varchar(100)

文档类型表

Column Type
Id int
Description varchar(100)

具有以下列的 JobTypeDocumentType:

Column Type
Id int
JobType int
DocumentType int

我想从数据库中检索数据并将结果记录集转换为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&lt;IEnumerable&lt;IObservable&lt;JobTypeDocumentTypeViewModel&gt;&gt;&gt; 类型的结果卡住了

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

【问题讨论】:

    标签: linq join rx.net


    【解决方案1】:

    在这种情况下你必须使用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)))
        );
    

    【讨论】:

    • 您的代码导致 IObservable> 但我正在尝试获取 IObservable>
    • 我的代码只返回 IEnumerable。谁转换为 IObservable?什么是 GetJobDocumentTypes?
    • 签名在代码中显示:IObservable&lt;IReadOnlyCollection&lt;JobTypeDocumentType&gt;&gt; dataService.GetJobDocumentTypes(int jobTypeId) 它返回一个 IObservable>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    相关资源
    最近更新 更多