已更新,但仍显示两个错误。
我得到的文档不是最好的,所以我不相信 Result/Ireadonlylist/HistValueResonseDTO 被定义,除非它是在依赖项之一中定义的。我尝试了以下代码,但出现了两个错误。 1.《严重性代码说明项目文件行抑制状态详细信息》 错误 CS1061“HistValueResponseDto”不包含“标签”的定义,并且找不到接受“HistValueResponseDto”类型的第一个参数的可访问扩展方法“标签”(您是否缺少 using 指令或程序集引用?) ConsoleApp7 C: \用户\jzala\源 epos\ConsoleApp7\Program.cs 89 Active" 和这个。 严重性代码 说明 项目文件行抑制状态详细信息 错误 CS1503 参数 1:无法从“方法组”转换为“对象?” ConsoleApp7 C:\Users\jzala\source epos\ConsoleApp7\Program.cs 89 活动
这是完整的代码。
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 = "XXXX";
const string password = "XXXXXXX";
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>> tagResults = await oneViewClient.GetHistTagsAsync(unitGuid, tags, dateFrom,
DateTO, 2, 2);
if (tagResults.IsFailure)
{
// An error occured
//Log.Logger.Error(tagResults.Error);
return;
}
// Path of output CSV File
string csvFilePath = "output.csv";
using (StreamWriter sw = new StreamWriter(csvFilePath))
{
sw.WriteLine("Tags,Count");
// Iterates over the results and writes them into the file
foreach (var d in tagResults.Value)
{
// Assuming d.Tags is a string and d.Count is an int
sw.WriteLine($"{d.Tags},{d.Count}");
}
// Parse the response body
//var dataObjects = tagResults;
//foreach (var d in dataObjects)
//{
// Console.WriteLine("{0},{0}", d.Tags,d.Count);
// }
//Log.Logger.Information("Historical events from OneView");
//tagResults.Value.ToList().ForEach(tag =>
//Log.Logger.Information(tagResults);
// Log.Logger.Information("Finish reading");
}
}
}
}
新代码结束
问题是因为您尝试使用
dataObjects
循环 foreach loop
,但 dataObjects
不符合必要的 IEnumerable
接口。您需要直接访问 tagResults
的 Value 属性才能循环访问 HistValueResponseDto
列表。
要将这些数据保存到
CSV file
,请首先创建文件流,写入标题,然后添加数据行。
这是调整后的代码,它迭代结果并将它们写入
CSV file
。
代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
// Assuming IOneViewClient, oneviewClientFactory, HistValueResponseDto, and Result<T> are defined elsewhere. Otherwise this won't work.
class Program {
static async Task Main(string[] args) {
IOneViewClient oneViewClient = oneviewClientFactory.CreateInstance();
Result < IReadOnlyList < HistValueResponseDto >> tagResults = await oneViewClient.GetHistTagsAsync(unitGuid, tags, dateFrom, DateTO, 2, 2);
if (tagResults.IsFailure) {
return;
}
// Path of output CSV File
string csvFilePath = "output.csv";
using(StreamWriter sw = new StreamWriter(csvFilePath)) {
sw.WriteLine("Tags,Count");
// Iterates over the results and writes them into the file
foreach(var d in tagResults.Value)
{
// Assuming d.Tags is a string and d.Count is an int
sw.WriteLine($"{d.Tags},{d.Count}");
}
}
}
}