是否可以插入LinqKit PredicateBuilder作为子查询?

时间:2016-10-24 14:06:13

标签: c# entity-framework predicate linqkit

我目前有一个可用的多级谓词......

var predicate = PredicateBuilder.New<Patient>();
foreach (var code in codeArray) // ect..["AB12", "W231", "Q213"]
{
    string localCode = code;
    predicate.Or(p => p.Requests.Any(
        u => u.Records.Any(
            t => t.OutgoingRecords.Any(
                s => s.Code == localCode)
            )
        )
    );
}

res = query.AsExpandable().Where(predicate); 
// Query is built up from an outside source and is eventually turned into a list via .ToListAsync()

除非我知道它以......的形式生成查询,否则这很好。

query =  p => p.PatientEpisodes.Any(u => u.EpisodeGroupings.Any(t => t.EpisodeDiagnoses.Any(s => s.Code == 'AB12'))) ||
              p.PatientEpisodes.Any(u => u.EpisodeGroupings.Any(t => t.EpisodeDiagnoses.Any(s => s.Code == 'W231'))) ||
              p.PatientEpisodes.Any(u => u.EpisodeGroupings.Any(t => t.EpisodeDiagnoses.Any(s => s.Code == 'Q213')))

当我真正应该制作的是......时,这有点过分(而且有点不对)

query =  p => p.PatientEpisodes.Any(u => u.EpisodeGroupings.Any(t => t.EpisodeDiagnoses.Any(s => s.Code == 'AB12' || s.Code == 'W312' || s.Code == 'Q213'))) 

有没有办法在查询的子级别插入谓词,如下所示?到目前为止,我目前的尝试失败了。

var predicate = PredicateBuilder.New<RecordCode>();
foreach (var code in codeArray)
{
    string localCode = code;
    predicate.Or(p => p.Code == localCode);
}

query = query.AsExpandable().Where(
    s => s.Requests.Any(
        c => c.Records.Any(
            d => d.OutgoingRecords.Any(predicate))));

非常感谢提前。

0 个答案:

没有答案