Skip to content

"Sequence contains more than one element" when trying to return a list of patients using AgeInYearsAt() #994

@Dylan-coder-arch

Description

@Dylan-coder-arch

Description:
When invoking a CQL library definition that returns a list of patients and uses AgeInYears() or AgeInYearsAt(), an exception occurs: "Sequence contains more than one element."

The generated C# assembly method signature correctly accepts IEnumerable, but inside the compiled filter logic, the generated code calls this.Patient instead of using the patient from the .Where LINQ clause, causing the evaluation to fail when multiple patients are passed.

To Reproduce:

  1. Create a basic CqlLibraryString with a define that gets a list of patients back and has the AgeInYearsAt() function. See the attached screenshot for the cql you can use.

  2. For the FHIR Bundle, you can create any type of valid bundle.

  3. Create a librarySetInvoker.

  4. Call .invokeLibraryDefinition on the librarySetInvoker with the above cql, definition name and library identifier.

  5. Try catch the method and view the error/exception.

Expected Behavior:
I expected this to return a list of patients that meet the criteria of the define. I.e all patients that are above a certain age are returned.

Screenshots:
cql:

library ControllingBloodPressure version '1.0.0'

using FHIR version '4.0.1'

parameter "Measurement Period" Interval<DateTime> default
  Interval [@2024-01-01T00:00:00.0, @2024-12-31T23:59:59.0]

context Patient

define "Eligible Population":
  [Patient] Primary
    where AgeInYearsAt(start of "Measurement Period") >= 18

C# of the LibrarySetInvoker invoking and error display:

CqlToolkit cqlToolkit = new();

var cql = (CqlLibraryString) cqlBundle;

var fhirLibrary = (CqlLibraryString) File.ReadAllText("resources/FHIRHelper.cql");

LibrarySetInvoker LibrarySetInvoker = cqlToolkit.AddCqlLibraries([cql]).CreateLibrarySetInvoker();
try
{
  LibrarySetInvoker.InvokeLibraryDefinition(
    cqlContext: FhirCqlContext.ForBundle(fhirBundle),
    LibraryIdentifier: cql.LibraryIdentifier,
    definitionSignature: "Eligible Population");
} catch (Exception ex)
{
  Console. WriteLine(ex. Message);
}

Version used:

  • Version: 2.1.0

Metadata

Metadata

Assignees

Labels

ElmToLinqOn HoldTickets that cannot immediately be picked up

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions