c# - C#:无法将存储在列表中的值写入控制台
问题描述
我具有以下功能,可让我通过此功能发出 REST 请求:
static Observation GetData(string baseUrl, string provider, string key, string sensor)
{
var client = new RestClient(String.Format("{0}/data/{1}/{2}", baseUrl, provider, sensor));
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("IDENTITY_KEY", key);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
var observationsModel = JsonSerializer.Deserialize<SentiloResponse>(response.Content, options);
Console.WriteLine("This is observationsModel:");
Console.WriteLine(observationsModel);
Console.WriteLine("This is the outcome of GetData:");
Console.WriteLine(observationsModel.Observations.FirstOrDefault());
return observationsModel.Observations.FirstOrDefault();
}
现在,从这一点
在下一部分中,我调用 GetData,我想选择值和时间戳变量来执行其他任务,但我不能(我的尝试是在声明 myTest 和 myTest_2 的行中):
static void Main(string[] args)
{
configuration = JsonSerializer.Deserialize<SentiloConfig>(File.ReadAllText("configuration.json"), options);
Console.WriteLine("Configuration Loaded");
foreach(var componentMapping in configuration.ComponentMappings)
{
var inputComponent = componentMapping.InputComponent;
Console.WriteLine("Getting data");
var sensorsContent = inputComponent.Sensors.AsParallel().Select(s =>
new SensorContent{
Sensor = GetSensorString(s.SensorName),
Observations = new List<Observation>() {
GetData(
inputComponent.ServerUrl,
inputComponent.Provider,
inputComponent.Token,
GetSensorString(s.SensorName)
)
}
}
).Where(s => s.Observations.First() != null).ToList();
var myTest = sensorsContent.Select(s => s.Observations.First().Value).ToList();
var myTest_2 = sensorsContent.Select(s => s.Observations.First().Value.ToList());
Console.WriteLine(myTest.ToString());
Console.WriteLine(myTest_2);
Console.WriteLine("data retrieved");
if (!sensorsContent.Any())
{
Console.WriteLine("No sensor data for component {0} with sensors: {1}, check configuration", inputComponent.ComponentName, string.Join(",", inputComponent.Sensors.Select(s => s.SensorName)));
continue;
}
var sensorRequest = new SentiloPutRequest { Sensors = sensorsContent };
}
System.Threading.Thread.Sleep(configuration.SendTime*1000);
}
但是 myTest 和 myTest_2 的结果如下:
System.Collections.Generic.List
1[System.String] System.Linq.Enumerable+SelectListIterator
2[SentiloSpoofer.SensorContent,System.Collections.Generic.List`1[System.Char]]
我在想也许最好在 Lambda 之外调用 GetData 并绕过这些问题,但如果我想检索这些数据,有什么好方法呢?
解决方案
这就是您写入控制台的方式。使用string.Join
或foreach
。
本质上,您正在尝试将值从列表中变出。从技术上讲,List<T>
没有覆盖ToString()
知道你想如何格式化它。由你决定。所以它只返回类型名称(这就是您所看到的)。
请尝试:
foreach(var value in myTest)
Console.WriteLine(value);
// or
Console.WriteLine(string.Join(",",myTest));
推荐阅读
- python - 将 bot.event() 打印到特定的不和谐频道
- excel - 使用单元格中的复选框在值 > 999 时静音蜂鸣声
- java - android studio中的按钮背景颜色总是紫色
- python - 如何在python中设置垂直输出?
- python - django-你忘记注册或加载这个标签了吗?
- angular - 面临以角度设置面包屑的问题
- oauth-2.0 - 在没有 PKCE 的情况下使用 OAuth2 授权码是什么情况
- pytorch - 从 pytorch 的 torchvision.datasets 在 Colab 中下载“MINST”
- python - 使用 Python 自动化带有 INIT 文件的命令行程序并为其提供命令
- python - 使用 Python elementTree 模块查找与具有另一个标记相同类别的 xml 标记的内容