import ballerina/io;type Student record {
    string firstName;
    string lastName;
    float score;
};type FullName record {|
    string firstName;
    string lastName;
|};public function main() {
    Student s1 = {firstName: "Alex", lastName: "George", score: 1.5};
    Student s2 = {firstName: "Ranjan", lastName: "Fonseka", score: 0.9};
    Student s3 = {firstName: "John", lastName: "David", score: 1.2};    Student[] studentList = [s1, s2, s3];    FullName[] nameList = [];
    var e = from var student in studentList
    do {
        FullName fullName = {firstName: student.firstName, lastName: student.lastName};
        nameList.push(fullName);
    };    foreach var name in nameList {
        io:println(name);
    }
}

Query Action

Like a query-expression, a query-action can be used with a value that is iterable with any error type. The result of the query-action is the termination value of the iterator. Thus, if the source type is infallibly iterable, the result will be () and can be ignored. This is a preview feature.

import ballerina/io;
type Student record {
    string firstName;
    string lastName;
    float score;
};
type FullName record {|
    string firstName;
    string lastName;
|};
public function main() {
    Student s1 = {firstName: "Alex", lastName: "George", score: 1.5};
    Student s2 = {firstName: "Ranjan", lastName: "Fonseka", score: 0.9};
    Student s3 = {firstName: "John", lastName: "David", score: 1.2};
    Student[] studentList = [s1, s2, s3];
    FullName[] nameList = [];
    var e = from var student in studentList

query-action works similarly to a foreach statement. It can be used to iterate through any iterable value. The result of the query-action is the termination value of the iterable value. It can be either an error or ().

    do {
        FullName fullName = {firstName: student.firstName, lastName: student.lastName};
        nameList.push(fullName);
    };

The block inside the do clause is executed for each iteration.

    foreach var name in nameList {
        io:println(name);
    }
}
# To run this sample, navigate to the directory that contains the
# `.bal` file and execute the `ballerina run` command.
$ ballerina run query_action.bal
firstName=Alex lastName=George
firstName=Ranjan lastName=Fonseka
firstName=John lastName=David