One IEnumerable with 1 item mysteriously becomes empty after I query a second IEnumerable

The variable pkeys has one item immediately after the query that returns it is executed, but then when the query that returns ckeys query is executed, suddenly pkeys shows Enumeration yielded no results while ckeys contains one item.
var pkeys= qry.GetTableColumns(tableName)
.Where(col => col.Kind == Column.ColumnKinds.PartitionKey)
.Select(col => new PropertyGenerationModel(col));
var ckeys= qry.GetTableColumns(tableName)
.Where(col => col.Kind == Column.ColumnKinds.Clustering)
.Select(col => new PropertyGenerationModel(col));
var pkeys= qry.GetTableColumns(tableName)
.Where(col => col.Kind == Column.ColumnKinds.PartitionKey)
.Select(col => new PropertyGenerationModel(col));
var ckeys= qry.GetTableColumns(tableName)
.Where(col => col.Kind == Column.ColumnKinds.Clustering)
.Select(col => new PropertyGenerationModel(col));
In case it helps, GetTableColumns is querying a Cassandra db like this:
using Cassandra.Mapping;
using CassandraClient.Config;
using CassandraClient.Metadata.Models;
namespace CassandraClient.Metadata;

public class MetaDataQueries(SessionOptions options)
{
public IEnumerable<Column> GetTableColumns(string tableName)
{
var session = SessionFactory.GetSession(options);
Mapper mapper = new(session);
var columns = mapper.Fetch<Column>(
$"select * from system_schema.columns where table_name = '{tableName}' and keyspace_name = '{session.Keyspace}' allow filtering;");
return columns;
}
}
using Cassandra.Mapping;
using CassandraClient.Config;
using CassandraClient.Metadata.Models;
namespace CassandraClient.Metadata;

public class MetaDataQueries(SessionOptions options)
{
public IEnumerable<Column> GetTableColumns(string tableName)
{
var session = SessionFactory.GetSession(options);
Mapper mapper = new(session);
var columns = mapper.Fetch<Column>(
$"select * from system_schema.columns where table_name = '{tableName}' and keyspace_name = '{session.Keyspace}' allow filtering;");
return columns;
}
}
5 Replies
Ꜳåąɐȁặⱥᴀᴬ
so you are saying pkeys is not materialized? what's the lifetime of SessionFactory.GetSession?
VoidPointer
VoidPointerOP7d ago
I would have said the session's lifetime is only for the duration of the GetTableColumns method, but now on closer inspection I see ISession is an IDiposable, and I don't have a using for it. Just maybe that has something to do with it. I just ended up slapping ToList() on the end of the queries and everything works nicely now.
SleepWellPupper
As an aside, It looks like you're opening yourself up for cql injection by using string interpolation here. Is there a way to parmeterize this query?
VoidPointer
VoidPointerOP6d ago
The code I posted here was just proof of concept, getting to know the DataStax Mapper for Cassandra, and I had just copied from some legacy code without checking anything. That code not using parameters, I didn't think of them yet. I have now parameterized all the queries after seeing that the Mapper methods nearly all have an overload with a params object[] args parameter. These args are used to fill in ? placeholders in the query string.
SleepWellPupper
Ah, I see. That's good then :)

Did you find this page helpful?