C# 将间隔时间样本写入文件
这是我的代码:C# 将间隔时间样本写入文件,c#,C#,这是我的代码: private static void LogTime(IList<TimeSpan> timeList) { TimeSpan frameTimeDuration = timeList[1] - timeList[0]; using (FileStream fs = new FileStream(@"C:\Users\practice\Desktop\LogPauseTime1.txt", FileMode.Append)) {
private static void LogTime(IList<TimeSpan> timeList)
{
TimeSpan frameTimeDuration = timeList[1] - timeList[0];
using (FileStream fs = new FileStream(@"C:\Users\practice\Desktop\LogPauseTime1.txt", FileMode.Append))
{
using (StreamWriter sw = new StreamWriter(fs))
{
for(int i = 0; i<timeList.Count(); i++)
{
for (int j = i; j < timeList.Count() - 1; j++)
{
if (timeList[j + 1] - timeList[j] == frameTimeDuration)
{
j = j + 1;
}
else
{
sw.WriteLine("{0} - {1}", timeList[i].ToString(), timeList[j].ToString());
i = i + 1;
i=j;
break;
}
}
}
}
}
}
。。。。
谁能告诉我我做错了什么
这是我的输出。结果应该是00:00:00.0260000-00:00:09.4820000
00:00:00.0260000 - 00:00:01.3840000
00:00:01.4110000 - 00:00:02.6640000
00:00:02.6910000 - 00:00:03.9440000
00:00:03.9710000 - 00:00:05.2240000
00:00:05.2510000 - 00:00:06.5040000
00:00:06.5310000 - 00:00:07.7840000
00:00:07.8110000 - 00:00:09.0640000
00:00:09.1170000 - 00:00:09.2730000
00:00:09.3260000 - 00:00:09.4820000
尝试此解决方案(如果我理解您的逻辑):
在linq版本中
private static void LogTime(IList<TimeSpan> timeList, TimeSpan maxFrameSize, string logFilePath)
{
var groupedTimes = timeList
.OrderBy(i=>i)
.GroupAdjacentBy((current, next) => next - current <= maxFrameSize)
.Select(g => new {FromTime = g.First(), ToTime = g.Last()});
var file = new System.IO.FileInfo(logFilePath);
using (var writer = file.AppendText())
{
foreach (var times in groupedTimes)
{
writer.WriteLine("{0} - {1}", times.FromTime, times.ToTime);
}
}
}
private static void LogTime(IList timeList,TimeSpan maxFrameSize,string logFilePath)
{
var groupedTimes=时间列表
.OrderBy(i=>i)
.GroupAdjacentBy((当前,下一个)=>next-current new{FromTime=g.First(),ToTime=g.Last()});
var file=new System.IO.FileInfo(logFilePath);
使用(var writer=file.AppendText())
{
foreach(分组时间中的var时间)
{
writer.WriteLine(“{0}-{1}”,times.FromTime,times.ToTime);
}
}
}
使用这个linq扩展
public static class LinqExtensions
{
public static IEnumerable<IEnumerable<T>> GroupAdjacentBy<T>(
this IEnumerable<T> source, Func<T, T, bool> predicate)
{
using (var e = source.GetEnumerator())
{
if (e.MoveNext())
{
var list = new List<T> { e.Current };
var pred = e.Current;
while (e.MoveNext())
{
if (predicate(pred, e.Current))
{
list.Add(e.Current);
}
else
{
yield return list;
list = new List<T> { e.Current };
}
pred = e.Current;
}
yield return list;
}
}
}
}
公共静态类LinqExtensions
{
公共静态IEnumerable groupAdjanceBy(
此IEnumerable源,Func谓词)
{
使用(var e=source.GetEnumerator())
{
if(如MoveNext())
{
var list=新列表{e.Current};
var pred=e.电流;
while(如MoveNext())
{
if(谓词(pred,e.Current))
{
列表。添加(如当前);
}
其他的
{
收益回报表;
列表=新列表{e.当前};
}
pred=e.电流;
}
收益回报表;
}
}
}
}
请将时间列表
以文本形式放入为什么中断代码>之前<代码>i=j代码>?您对分组的预期方法是什么?如何判断正确的帧时长
?帧时-这是两个时间值之间的最小可能值。您当前的输出是什么?
PrintLog(timeList, TimeSpan.FromSeconds(1) /* or timeList[1] - timeList[0] */, @"D:\Temp\output.txt" /* or your file name */);
private static void LogTime(IList<TimeSpan> timeList, TimeSpan maxFrameSize, string logFilePath)
{
var groupedTimes = timeList
.OrderBy(i=>i)
.GroupAdjacentBy((current, next) => next - current <= maxFrameSize)
.Select(g => new {FromTime = g.First(), ToTime = g.Last()});
var file = new System.IO.FileInfo(logFilePath);
using (var writer = file.AppendText())
{
foreach (var times in groupedTimes)
{
writer.WriteLine("{0} - {1}", times.FromTime, times.ToTime);
}
}
}
public static class LinqExtensions
{
public static IEnumerable<IEnumerable<T>> GroupAdjacentBy<T>(
this IEnumerable<T> source, Func<T, T, bool> predicate)
{
using (var e = source.GetEnumerator())
{
if (e.MoveNext())
{
var list = new List<T> { e.Current };
var pred = e.Current;
while (e.MoveNext())
{
if (predicate(pred, e.Current))
{
list.Add(e.Current);
}
else
{
yield return list;
list = new List<T> { e.Current };
}
pred = e.Current;
}
yield return list;
}
}
}
}