1

I've currently got a working multi level predicate shown below...

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()

Which is fine except as far as I'm aware it is producing a query in the form of...

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')))

Which is just overkill (and somewhat wrong) when all I really should be producing is...

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

Is there a way to insert the predicate at a sub level of a query such as below? My current attempts to do so have failed so far.

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

Many thanks in advance.

Scott Alexander
  • 455
  • 2
  • 17

0 Answers0