'Value to add was out of range. (Parameter 'value')'.
У меня проблемы с ToList и IEnumerable при использовании TDMSReader от Nuget. Я попытался l oop через IEnumerable и добавить в новый список, но основная ошибка исходит от ForEach l oop. Я также пробовал ToList, который выдает ту же ошибку.
Я не могу прочитать значение, которое выдает ошибку, как это происходит в foreach l oop, прежде чем я доберусь до следующего «элемента». Из всего, что я нашел, это, вероятно, ошибка минимального / максимального времени DateTime, но я не могу найти ошибку в данных и попытался удалить любые даты между 2000 и 2021 годами. Я могу получить до 2961 записей в «да» перед ошибкой. В канале 6204 записей.
using LambdaTdms.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TdmsMaster.Managers
{
public class StackOverflow
{
public void Main()
{
string basePath = Functions.GetBasePath();
string filePath = Functions.GetNewFilePath(basePath);
if (filePath == null)
{
return;
}
using (NationalInstruments.Tdms.File file = new NationalInstruments.Tdms.File(filePath))
{
try
{
file.Open();
List<PutObjectResponseModel> models = ByChannel(file);
file.Dispose();
}
catch (Exception ex)
{
//Error handling
}
}
}
private List<PutObjectResponseModel> ByChannel(NationalInstruments.Tdms.File file)
{
List<PutObjectResponseModel> responseModels = new List<PutObjectResponseModel>();
foreach (var group in file.Groups)
{
foreach (NationalInstruments.Tdms.Channel channel in group.Value)
{
string fileName = $"{FormatPath(group.Key)}_{ FormatPath(channel.Name)}";
IEnumerable<DateTime> datas = channel.GetData<DateTime>();
//Error: datas.ToList();
List<DateTime> da = new List<DateTime>();
try
{
datas = datas.OrderBy(c => c.Year)
.ThenBy(c => c.Month)
.ThenBy(c => c.Day)
.ThenBy(c => c.Hour)
.ThenBy(c => c.Minute)
.ThenBy(c => c.Second)
.ThenBy(c => c.Millisecond);
datas = datas.Where(p => p.Year > 2000).Where(p => p.Year < 2021);
//Error: foreach (DateTime item in datas)
foreach (DateTime item in datas)
{
DateTime dt = (DateTime)item;
if (dt == null)
{
Console.WriteLine(item);
}
else
{
da.Add(item);
}
}
}
catch
{
//handler error thrown from *foreach (DateTime item in datas)
}
}
}
return responseModels;
}
private string FormatPath(string path)
{
path = path.Replace("/", " ").Replace("?", "").Replace("*", "");
return path;
}
}
}