首页 > 解决方案 > 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+SelectListIterator2[SentiloSpoofer.SensorContent,System.Collections.Generic.List`1[System.Char]]

我在想也许最好在 Lambda 之外调用 GetData 并绕过这些问题,但如果我想检索这些数据,有什么好方法呢?

标签: c#listlambda

解决方案


这就是您写入控制台的方式。使用string.Joinforeach

本质上,您正在尝试将值从列表中变出。从技术上讲,List<T>没有覆盖ToString()知道你想如何格式化它。由你决定。所以它只返回类型名称(这就是您所看到的)。

请尝试:

foreach(var value in myTest)
  Console.WriteLine(value);

// or
Console.WriteLine(string.Join(",",myTest));

推荐阅读