There are a few issues in the code snippet you provided:
- The
tables
variable may not have the same number of elements as the number of variables (id
,measure
,val
,time
) you are trying to extract from it. This means that when you try to access an element from one of these variables, an IndexOutOfRangeException may occur. - The
val
variable is being accessed from index 3, however it's not clear if the data is stored at this index, so this could cause an IndexOutOfRangeException. - In the for loop where you are iterating through the count, you are assuming that the number of records in the
id
variable is the same as the number of records in the other variables, this could cause an IndexOutOfRangeException if the number of records are different. - The
time[i].GetTimeInDateTime()
the method is not a valid method and this will cause a runtime error. - The
measure[i].GetValue()
the method is also not a valid method and this will cause a runtime error. - The
val[i].GetValue()
method is also not a valid method and this will cause a runtime error.
below is the code
Here's an example of how you can fix the above code to correctly extract data from the InfluxDB:
Copy code
using System;
using System.Linq;
using System.Threading.Tasks;
using InfluxDB.Client;
using InfluxDB.Client.Api.Service;
using InfluxDB.Client.Core;
using InfluxDB.Client.Writes;
namespace ServerSide
{
public class ReadInfluxDB
{
private readonly string Token = "TestTokenWEBICC";
public async Task Main()
{
var client = new InfluxDBClient("http://localhost:8086", Token);
var queryApi = client.GetQueryApi();
//Query
var flux = "from(bucket:\"TestDB\") |> range(start: 0)";
var tables = await queryApi.QueryAsync(flux);
if(tables.Count == 0)
{
Console.WriteLine("No data found");
return;
}
//Extract data
var records = tables[0].Records;
var count = records.Count();
var values = new List<Value>();
for (int i = 0; i < count; i++)
{
var record = records[i];
var time = record.GetTime();
var value = record.GetValue();
var measurement = record.GetMeasurement();
values.Add(new Value(Convert.ToDateTime(time), measurement, Convert.ToDouble(value)));
}
values.ForEach(val => Console.WriteLine(val.ToString()));
client.Dispose();
}
}
public class Value
{
public DateTime DateAndTime { get; set; }
public string Measure { get; set; }
public double Val { get; set; }
public Value(DateTime dateAndTime, string measure, double val)
{
DateAndTime = dateAndTime;
Measure = measure;
Val = val;
}
public override string ToString()