shayanmalinda
shayanmalinda

Reputation: 2287

Querying distinct values in Ballerina Query Expressions

Is it possible to query DISTINCT values using Ballerina Query Expressions?

string[]|error connectorProducts = from record {string product;} {product} in products
        select product;

connectorProducts should only have DISTINCT values at the end.

Thanks.

Upvotes: 1

Views: 103

Answers (2)

MaryamZi
MaryamZi

Reputation: 1304

This can be achieved by incorporating a group by clause.

from record {string product;} {product} in products
      group by product
      select product;

Also see

Note that there's an open spec issue to look into the possibility of adding an array:unique function as a straightforward alternative for when we just want to filter out unique members from an array.

Upvotes: 4

Sasindu Dilshara
Sasindu Dilshara

Reputation: 192

There is no direct way to do this. but you can use the group by clause and do this operation like this

import ballerina/io;

record {string product;}[] products = [{ product: "a"}, { product: "b"}, { product: "a"}, { product: "c"}, { product: "a"}];

function test() {
    string[]|error connectorProducts = from record {string product;} {product} in products
        group by product
        select product;
    
    io:println(connectorProducts);
}

public function main() {
    test();
}

Upvotes: 1

Related Questions