使用c# linq,在where子句中只包含前一个where子句的结果

guz6ccqo  于 2022-12-20  发布在  C#
关注(0)|答案(2)|浏览(175)

我只想从_vwSourceEndpoints检索的sourceEndpoints获取SourceEndpointConfigs。我不确定如何修改where子句以获取正确的结果。感谢所有帮助。谢谢!

_vwSourceEndpoints = db.vwSourceEndpoints
    .Where(e => e.StudySourceSystemId == study.StudySourceSystemId
            & e.ScheduleId == study.ScheduleId
            & e.OrderNo == study.OrderNo
            & e.Active == true
    ).ToList();

_vwSourceEndpointConfigs = db.SourceEndpointConfigs.Where(
    e => e.Active == true
    ).ToList();

var query3 = _vwSourceEndpointConfigs.Concat(_vwSourceEndpoints.OrderBy(x => x.OrderNo).ToList();

我已经想出了以下作为一个编辑我的原始代码

_vwSourceEndpoints = db.vwSourceEndpoints.Where(
                    e => e.StudySourceSystemId == study.StudySourceSystemId
                    & e.ScheduleId == study.ScheduleId
                    & e.OrderNo == study.OrderNo
                    & e.Active == true).ToList();

                _vwSourceEndpointConfigs = db.SourceEndpointConfigs.Where(
                    e => _vwSourceEndpoints.All(x => x.StudySourceSystemId == study.StudySourceSystemId
                   & x.ScheduleId == study.ScheduleId
                   & x.OrderNo == study.OrderNo
                   & x.Active == true)
                    

                    ).ToList();
5kgi1eie

5kgi1eie1#

如果两个表具有相同的列,则可以使用“联合”,否则选择所需的列,然后使用“联合”。

var query = context.vwSourceEndpoints
                        .Where(e => e.StudySourceSystemId == study.StudySourceSystemId
                                && e.ScheduleId == study.ScheduleId
                                && e.OrderNo == study.OrderNo
                                && e.Active == true
                        ).Union(context.SourceEndpointConfigs.Where(e => e.Active == true);
3okqufwl

3okqufwl2#

这是我最终发现的最好的工作解决方案。

_vwSourceEndpoints = db.vwSourceEndpoints.Where(
                    e => e.StudySourceSystemId == study.StudySourceSystemId
                    & e.ScheduleId == study.ScheduleId
                    & e.OrderNo == study.OrderNo
                    & e.Active == true).ToList();

                _vwSourceEndpointConfigs = (from config in db.SourceEndpointConfigs.Where(c => c.Active == true)
                                            join ep in _vwSourceEndpoints
                                            on config.SourceEndpointId equals ep.SourceEndpointId
                                            select config).ToList();

相关问题