如何解决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,我想选择Value和Timestamp Variables来执行其他任务,但是我无法执行(我的尝试在声明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()
的 override ,它知道如何设置其格式。由你决定。因此,它只返回类型名称(这就是您所看到的)。
请尝试:
foreach(var value in myTest)
Console.WriteLine(value);
// or
Console.WriteLine(string.Join(",",myTest));
,
您正在尝试以字符串形式访问列表。 如果要在控制台中显示这些值,则只需在列表中使用汇总即可。
示例:
var myTestString = myTest.Select(i => i).Aggregate((i,j) => i + "," + j);
Console.WriteLine(myTestString)
// Or
myTest.ForEach(x => Console.WriteLine(x));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。