Search code examples
c#cassandradatastax-csharp-driver

Can Cassanda aggregate LINQ expression in its query?


I am quite new to Cassandra so I have a question that I can not find answer to. In EF Core I can pass a list of LINQ expression as conditions and aggregate them so I can find what I need for example:

public async Task<IEnumerable<string>> GetDataStream(List<Expression<Func<Model, bool>>> predicates)
{
    var query = _context.Model.AsQueryable();
    if (predicates != null)
    {
        query = predicates.Aggregate(query, (@event, condition) => @event.Where(condition));
    }
    return await query.Select(data => data.).ToListAsync();
} 

Now I am wondering if there is a such possibility in Cassandra. I tried:

public async Task<IEnumerable<Model>> Find(List<Expression<Func<Model, bool>>> predicates, int assetId)
{
    IQueryable<Model> query = _table.AsQueryable();
    if (predicates != null)
    {
        query = predicates.Aggregate(query, (@event, condition) => @event.Where(condition));
    }

    return await query.Select(data => data); // here is a problem dont know ow to execute this
}

So is such a thing possible?

EDIT:

So I tried with aggregate combination

query.Select(d => d).Execute();

also and got this exception in result

The expression Call = [SELECT gap_end, gap_start, uuid FROM gaps_state_data.Where(data => (data.EndValue == null))] is not supported in None parse phase.

It looks expression aggregate is not being format for some reason.


Solution

  • Ok, I figured this one out, using hint in comments, let me show full code of this and how it should work:

    public Task<IEnumerable<Model>> Find(List<Expression<Func<Model, bool>>> predicates)
    {
        CqlQuery<Model> query = _table.Select(d => d);
    
        if (predicates != null)
        {
            query = predicates.Aggregate(query, (@event, condition) => @event.Where(condition));
        }
    
        return query.ExecuteAsync();
    }
    

    As I needed to replace table type with IQueryable, but when I do this early on like _table.AsQueryable() select expression compiled itself. So I needed start expression that will change table to IQueryable and that is the role of that Select(). Afterwards I could add expressions from parameter.