Swan Lake Beta3 is here.
import ballerina/io;

type Employee record {|
    readonly int id;
    string firstName;
    string lastName;
    int salary;
|};

public function main() {
    table<Employee> key(id) employees = table [
        {id: 1, firstName: "John", lastName: "Smith", salary: 100},
        {id: 2, firstName: "Fred", lastName: "Bloggs", salary: 2000}
    ];

    // The query expression starts with `table`.
    // The key specifier `key(id)` specifies the key sequence of the constructed `table`.
    // The result of the query expression is a `table`.
    var highPaidEmployees = table key(id) from var e in employees
                            where e.salary >= 1000
                            select e;


    io:println(highPaidEmployees);
}

Creating Tables With Query

Query expressions can create tables. The key of the created table can be specified explicitly.

import ballerina/io;
type Employee record {|
    readonly int id;
    string firstName;
    string lastName;
    int salary;
|};
public function main() {
    table<Employee> key(id) employees = table [
        {id: 1, firstName: "John", lastName: "Smith", salary: 100},
        {id: 2, firstName: "Fred", lastName: "Bloggs", salary: 2000}
    ];
    var highPaidEmployees = table key(id) from var e in employees
                            where e.salary >= 1000
                            select e;

The query expression starts with table. The key specifier key(id) specifies the key sequence of the constructed table. The result of the query expression is a table.

    io:println(highPaidEmployees);
}
bal run creating_tables_with_query.bal
[{"id":2,"firstName":"Fred","lastName":"Bloggs","salary":2000}]
import ballerina/io;

type Employee record {|
    readonly int id;
    string firstName;
    string lastName;
    int salary;
|};

public function main() {
    table<Employee> key(id) employees = table [
        {id: 1, firstName: "John", lastName: "Smith", salary: 100},
        {id: 2, firstName: "Fred", lastName: "Bloggs", salary: 2000}
    ];

    // The query expression starts with `table`.
    // The key specifier `key(id)` specifies the key sequence of the constructed `table`.
    // The result of the query expression is a `table`.
    var highPaidEmployees = table key(id) from var e in employees
                            where e.salary >= 1000
                            select e;


    io:println(highPaidEmployees);
}

Creating Tables With Query

Query expressions can create tables. The key of the created table can be specified explicitly.

import ballerina/io;
type Employee record {|
    readonly int id;
    string firstName;
    string lastName;
    int salary;
|};
public function main() {
    table<Employee> key(id) employees = table [
        {id: 1, firstName: "John", lastName: "Smith", salary: 100},
        {id: 2, firstName: "Fred", lastName: "Bloggs", salary: 2000}
    ];
    var highPaidEmployees = table key(id) from var e in employees
                            where e.salary >= 1000
                            select e;

The query expression starts with table. The key specifier key(id) specifies the key sequence of the constructed table. The result of the query expression is a table.

    io:println(highPaidEmployees);
}
bal run creating_tables_with_query.bal
[{"id":2,"firstName":"Fred","lastName":"Bloggs","salary":2000}]
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand