import ballerina/io;type Student record {
    string firstName;
    string lastName;
    int intakeYear;
    float score;
};type Report record {
    string name;
    string degree;
    int expectedGradYear;
};public function main() {    Student s1 = {firstName: "Alex", lastName: "George", intakeYear: 2020, score: 1.5};
    Student s2 = {firstName: "Ranjan", lastName: "Fonseka", intakeYear: 2020, score: 0.9};
    Student s3 = {firstName: "John", lastName: "David", intakeYear: 2022, score: 1.2};    Student[] studentList = [s1, s2, s3];
    Report[] reportList = from var student in studentList
       where student.score >= 1
       let string degreeName = "Bachelor of Medicine",
       int graduationYear = calGraduationYear(student.intakeYear)
       select {
              name: student.firstName,
              degree: degreeName,
              expectedGradYear: graduationYear
       };    foreach var report in reportList {
        io:println(report);
    }
}function calGraduationYear(int year) returns int {
    return year + 5;
}

Query Expression

A query expression is a list comprehension, which can be used with a value that is iterable with any error type. It consists of four kinds of clauses: from, let, where, and select. The first clause must be a from clause and the last clause must be a select. The result of the query expression is a list. This is a preview feature.

import ballerina/io;
type Student record {
    string firstName;
    string lastName;
    int intakeYear;
    float score;
};
type Report record {
    string name;
    string degree;
    int expectedGradYear;
};
public function main() {
    Student s1 = {firstName: "Alex", lastName: "George", intakeYear: 2020, score: 1.5};
    Student s2 = {firstName: "Ranjan", lastName: "Fonseka", intakeYear: 2020, score: 0.9};
    Student s3 = {firstName: "John", lastName: "David", intakeYear: 2022, score: 1.2};
    Student[] studentList = [s1, s2, s3];
    Report[] reportList = from var student in studentList

The from clause works similarly to a foreach statement. It can be used to iterate any iterable value. The outputStudentList is the result of the query expression.

       where student.score >= 1

The where clause provides a way to perform conditional execution and works similarly to an if condition. It can refer to variables bound by the from clause. When the where condition evaluates to false, the iteration skips following the clauses.

       let string degreeName = "Bachelor of Medicine",
       int graduationYear = calGraduationYear(student.intakeYear)

The let clause binds the variables.

       select {
              name: student.firstName,
              degree: degreeName,
              expectedGradYear: graduationYear
       };

The select clause is evaluated for each iteration. The result of the query expression is a list(reportList) whose members are the result of the select clause.

    foreach var report in reportList {
        io:println(report);
    }
}
function calGraduationYear(int year) returns int {
    return year + 5;
}
# To run this sample, navigate to the directory that contains the
# `.bal` file and execute the `ballerina run` command.
$ ballerina run query_expression.bal
name=Alex degree=Bachelor of Medicine expectedGradYear=2025
name=John degree=Bachelor of Medicine expectedGradYear=2027