如何在 C# 中解构多个嵌套列表

问题描述 投票:0回答:1

我正在从 API 中提取数据,结果是非常混乱的列表,其中有一些嵌套列表。我的总体目标是让代码输出一个 .csv 文件,其中每行都有单位、标签、日期时间和值。我弄清楚了第一个列表,但其他嵌入的列表超出了我的理解范围。返回的数据位于名为 tagResult 的列表中,其中包含一个名为 value 的列表。在值列表中,它有标记名(我想要的)和称为值的列表。值列表包含时间戳和我想要的实际值。附件是断点的屏幕截图。

Data Structure

这是我当前的代码。

namespace ConsoleApp7
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    using System.Numerics;
    using System.Threading.Tasks;
    using CSharpFunctionalExtensions;
    using CsvHelper;
    using Microsoft.IdentityModel.Tokens;
    using Newtonsoft.Json.Linq;
    using Scada.OneView.Api.Client;
    using Scada.OneView.Api.Client.Abstractions;
    using Scada.OneView.Api.Client.Models.HistData;
    using Serilog;
    class Program
    {
        static async Task Main(string[] args)
        {
            // Initialize parameters
            const string oneViewUrl = "http://192.168.1.11/oneview4";
            const string authenticationUrl = "http://192.168.1.11/oneview4/auth/";
            const string username = "XXX";
            const string password = "SXXXXX";
            const string logFilePath = @"C:\Users\jzala\Documents\PivotGenPy\test7.txt";

            // Initialize logger
            Log.Logger = new LoggerConfiguration()
            .WriteTo.Console()
            .WriteTo.File(logFilePath)
            .CreateLogger();

            // Put OneView Authentication URL and username/password here
            var authenticationSettings = new OneViewAuthenticationSettings
            {
                AuthUrl = authenticationUrl,
                Username = username,
                Password = password,
                AuthenticationType = AuthenticationType.IdentityAuthentication,
            };

            // Put OneView URL here
            var oneviewSettings = new OneViewClientSettings
            {
                OneViewUrl = oneViewUrl
            };

            // Create single factory for one OneView, using oneview and authentication settings
            using var oneviewClientFactory = new OneViewClientFactory(
            oneviewSettings,
            authenticationSettings,
            Log.Logger);


            //Historical Values
            var unitGuid = new Guid("d6f49127-d710-4730-9aa1-d44846fd5537");
            var tags = new List<string>() { "WCNV.GriA2_10m_Max", "WCNV.GriA2_10m_Min", "WCNV.GriA2_10m_Std" };
            var dateFrom = new DateTime(2024, 3, 1, 10, 0, 0);
            var DateTO = new DateTime(2024, 3, 5, 0, 0, 0);
            const byte amountOfEvents = 90;


            IOneViewClient oneViewClient = oneviewClientFactory.CreateInstance();

            Result<IReadOnlyList<HistValueResponseDto>> tagResult = await oneViewClient.GetHistTagsAsync(unitGuid, tags, dateFrom,
                DateTO, 2, 2);


            if (tagResult.IsFailure)
            {
                // An error occured
                //Log.Logger.Error(tagResults.Error);
                return;
            }

            // Path of output CSV File
            string csvFilePath = @"C:\Users\jzala\Documents\PivotGenPy\test17.txt";

            using (StreamWriter sw = new StreamWriter(csvFilePath))
            {
                sw.WriteLine("Tags,Count");
                sw.WriteLine(tagResult);

                // Iterates over the results and writes them into the file
               foreach (var d in tagResult.Value.ToList())
                {
                    //Assuming d.Value is a string and d.Count is an int
                    //sw.WriteLine($"{d.Value},{d.Count");
                    sw.WriteLine(d.TagName);
                }

                var test = tagResult.Value.ToList();
              

                //Log.Logger.Information("Historical events from OneView");
                tagResult.Value.ToList().ForEach(Value =>
                Log.Logger.Information("{@Unit},{@Tagname},{@DTTM},{@Value}",Value.TagName,Value.Values.ToList()));
                // Log.Logger.Information("Finish reading");
            }
        }
    }
}

c# export-to-csv nested-lists
1个回答
0
投票

我的一位在 XM 负责软件工程的朋友昨晚帮助了我。所以这里是如何取消数组和列表中的嵌套列表的答案。

        var dateFrom = new DateTime(2024, 3, 1, 0, 0, 0);
    var DateTO = new DateTime(2024, 3, 11, 0, 0, 0);
    const byte amountOfEvents = 90;
    IOneViewClient oneViewClient = oneviewClientFactory.CreateInstance();
    Result<IReadOnlyList<HistValueResponseDto>> tagResult = await oneViewClient.GetHistTagsAsync(unitGuid, tags, dateFrom,
        DateTO, 10, 0);
    if (tagResult.IsFailure)
    {
        // An error occured
        Log.Logger.Error(tagResult.Error);
        return;
    }
    //Log.Logger.Information("Historical events from OneView");
    tagResult.Value.ToList().ForEach(Value =>
        Value.Values.ToList().ForEach(Values =>
       Log.Logger.Information("{@Unit},{@tagname},{@Timestamp},{@Value}", unitGuid, Value.TagName, Values.Timestamp, Values.Value)));
}
Log.Logger.Information("done");

}

© www.soinside.com 2019 - 2024. All rights reserved.