c#匹配两个列表,然后将两个列表之间的时间相加

c#匹配两个列表,然后将两个列表之间的时间相加,c#,list,compare,add,C#,List,Compare,Add,我有两张名单和一个班级 public class CommonLog { public string Break { get; set; } public string Cart { get; set; } public string Length { get; set; } } 这是清单一 commonlog.Add(new CommonLog { Break = breakTimeVar, Cart = cartVar, Length = lengthHou

我有两张名单和一个班级

public class CommonLog
{
    public string Break { get; set; }
    public string Cart { get; set; }
    public string Length { get; set; }
}
这是清单一

commonlog.Add(new CommonLog { Break = breakTimeVar, Cart = cartVar,
    Length = lengthHours });
还有一个像这样的列表2

commonlog2.Add(new CommonLog { Break = breakTimeVar2, Cart = cartVar2,
    Length = lengthHours2 });
我需要匹配的两条信息如下

清单1包含了这一点

0016 009130 00:01:30
0016 0066486 00:00:30

0016 0050093 00:00:30

0016 0063791 00:00:30
清单2包含了这一点

0016 009130 00:01:30
0016 0066486 00:00:30

0016 0050093 00:00:30

0016 0063791 00:00:30
我需要匹配两个列表中的第一个数字0016,然后将列表2中的最后一个数字00:00:30(3 x 30秒)相加,并将该总时间与列表1的总时间进行比较,然后根据列表2中的最后一个数字(时间)的总和是否等于列表1做出决定


我将如何实现这一点

您可以使用以下命令对单个中断进行分组,然后循环遍历聚合中断以查找匹配项

总的来说,每个断裂都有

对于
长度
,我建议使用而不是
字符串

资料


您可以使用以下命令对单个中断进行分组,然后循环遍历聚合中断以查找匹配项

总的来说,每个断裂都有

对于
长度
,我建议使用而不是
字符串

资料


这是一个LINQ解决方案,它以与Romoku答案类似(但更紧凑)的方式聚合列表2条目:

var groupedLogs = commonlog2
    .GroupBy(c => c.Break, c => TimeSpan.Parse(c.Length))
    // group logs by Break, and get the TimeSpan representation of Length
    // for each entry of the group
    .ToDictionary(g => g.Key, g => g.Aggregate(TimeSpan.Zero, (s, c) => s + c));
    // create a dictionary and aggregate each log group into sums of TimeSpans
然后,您可以遍历
commonlog
的每个项目,并比较结果:

foreach(var log in commonlog)
{
    TimeSpan sum;
    groupedLogs.TryGetValue(log.Break, out sum);
    if(sum == TimeSpan.Parse(log.Length))
    {
        // do something
    }
}
或者一种仅从
commonlog
中获取匹配项的单行方式(使用C#7功能):


这是一个LINQ解决方案,它以与Romoku答案类似(但更紧凑)的方式聚合列表2条目:

var groupedLogs = commonlog2
    .GroupBy(c => c.Break, c => TimeSpan.Parse(c.Length))
    // group logs by Break, and get the TimeSpan representation of Length
    // for each entry of the group
    .ToDictionary(g => g.Key, g => g.Aggregate(TimeSpan.Zero, (s, c) => s + c));
    // create a dictionary and aggregate each log group into sums of TimeSpans
然后,您可以遍历
commonlog
的每个项目,并比较结果:

foreach(var log in commonlog)
{
    TimeSpan sum;
    groupedLogs.TryGetValue(log.Break, out sum);
    if(sum == TimeSpan.Parse(log.Length))
    {
        // do something
    }
}
或者一种仅从
commonlog
中获取匹配项的单行方式(使用C#7功能):


匹配列表1和2中的所有内容,或者只匹配特定值?为什么不存储为int/TimeSpan而不是字符串?列表1中的第一个数字是否重复?如果是这样,您想将列表2中的总和与它们中的每一个进行比较吗?我同意@YairHalberstadt,至少如果
Length
TimeSpan
的话,那么求和就会容易得多。实际上,您需要解析值以进行求和。列表中还有更多内容,因此列表1有0036 009130 00:01:30,列表2有0036 0063791 00:00:30 0036 0064592 00:00:20 0036 0076290 00:00:50,所以在上的下一个列表循环中,我想查找0036中断中的所有cart,并将列表2与cart 0036匹配的时间相加正在从两个文本文件中读取此数据。然后当前存储在一个列表中,我愿意以不同的方式来做,感谢列表1和2中的所有内容,或者仅仅是一个特定的值?为什么不存储为int/TimeSpan而不是字符串?列表1中的第一个数字是否有重复项?如果是这样,您想将列表2中的总和与它们中的每一个进行比较吗?我同意@YairHalberstadt,至少如果
Length
TimeSpan
的话,那么求和就会容易得多。实际上,您需要解析值以进行求和。列表中还有更多内容,因此列表1有0036 009130 00:01:30,列表2有0036 0063791 00:00:30 0036 0064592 00:00:20 0036 0076290 00:00:50,所以在上的下一个列表循环中,我想查找0036中断中的所有cart,并将列表2与cart 0036匹配的时间相加正在从两个文本文件中读取此数据。然后存储在当前列表中,我愿意以不同的方式进行操作,谢谢