Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to generate tuple matching query based on multiple column? #730

Open
md-redwan-hossain opened this issue Oct 15, 2024 · 1 comment
Open

Comments

@md-redwan-hossain
Copy link

Is there any way to generate queries as given below?? I did't find anything that can achieve as the followings.

SELECT * 
FROM "ProductStocks" ps
where (ps."DepotId", ps."ProductId", ps."ProductBatchId") IN ((1, 1, 1),(1, 1, 2));

SELECT *
FROM person
WHERE (firstname, lastname) = ( ('Arthur', 'Dent') );

SELECT *
FROM person
WHERE (firstname, lastname) > ('Arthur', 'Dent');

SELECT *
FROM person
WHERE (firstname, lastname) IN ( ('Arthur', 'Dent'), ('Ford','Prefect') )
@keparlak
Copy link

First you can use raw query like;

var query = new Query("ProductStocks")
    .WhereRaw("(\"DepotId\", \"ProductId\", \"ProductBatchId\") IN ((1, 1, 1),(1, 1, 2))");

or you can use like the example below;

// Tuple listesi için
var tuples = new List<(int depotId, int productId, int batchId)> 
{
    (1, 1, 1),
    (1, 1, 2)
};

var query = new Query("ProductStocks");
query.Where(q =>
{
    foreach (var tuple in tuples)
    {
        q.OrWhere(sub => sub
            .Where("DepotId", tuple.depotId)
            .Where("ProductId", tuple.productId)
            .Where("ProductBatchId", tuple.batchId));
    }
});

Maybe you can create a custom extension method to do this like that;

public static class SqlKataExtensions
{
    public static Query WhereInTuple(this Query query, 
        string[] columns, 
        IEnumerable<object[]> values)
    {
        var conditions = string.Join(", ", columns.Select(c => $"\"{c}\""));
        var valuesList = string.Join(",", 
            values.Select(v => $"({string.Join(", ", v)})"));
        
        return query.WhereRaw($"({conditions}) IN ({valuesList})");
    }
}

// Usage:
var query = new Query("ProductStocks")
    .WhereInTuple(
        new[] { "DepotId", "ProductId", "ProductBatchId" },
        new[] 
        { 
            new object[] { 1, 1, 1 },
            new object[] { 1, 1, 2 }
        }
    );
    
    ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants