I've got a database (Cassandra) query that returns an IEnumerable. Trying to track down why this was returning no data (when I know there's data in the database) I found a curious issue.
The query does in fact return data, 25 entries. I was checking this with a Data.Count(); But later in the code, it was empty. I realised that the Count Method was inexplicably clearing the data.
After a quick investigation: Any for of reading this data clears it completely. Even in the debug, if I load the "results view" to get the list of data, initially I see my 25 entries - then if I click off, and then reload the results view: Emtpy.
Anyone ever had anything like this before?
String connectionString = "SELECT * FROM thedatabase WHERE thecondition";
RowSet Data = ExecuteComand(connectionString);
if (Data == null) // interestingly, I can check for null without issue
{
OutputMessage("Output to " + exportFile + " failed");
return;
}
int b = Data.Count(); // results in 25
int c = Data.Count(); // results in 0
I don't know cassandra but i assume that the returned IEnumerable<T> is executed lazily(deferred). By consuming it (f.e. with foreach, ToList or Count) the query gets executed and the resources(f.e. the connection) are disposed/closed.
If that's the case you could load it into an in memory collection, for example:
var data = ExecuteComand("SELECT * FROM thedatabase WHERE thecondition").ToList();
Now you can use data.Count without "clearing" it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With