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

c# Match between two lists, then add up time in between the two lists

我有两个列表和一个 class

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

列表 2 包含这个

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

做出决定

我将如何实现?

您可以使用 GroupBy 对单个中断进行分组,然后循环遍历聚合中断以找到匹配项。

将各个休息时间加起来有 Aggregate

我建议使用 TimeSpan 而不是 string 作为 Length

数据

var totalBreaks = new List<CommonLog>
{
    new CommonLog
    {
        Break = "0016",
        Cart = "009130",
        Length = "00:01:30"
    }
};

var individualBreaks = new List<CommonLog>
{
    new CommonLog
    {
        Break = "0016",
        Cart = "0066486",
        Length = "00:00:30"
    },
    new CommonLog
    {
        Break = "0016",
        Cart = "0050093",
        Length = "00:00:30"
    },
    new CommonLog
    {
        Break = "0016",
        Cart = "0063791",
        Length = "00:00:30"
    }
};

逻辑

//Group the individual breaks by their Break
var breakGroups = individualBreaks.GroupBy(x => x.Break);

// Loop through the aggregates
foreach (var totalBreak in totalBreaks)
{
    // Match the aggregate to the individual
    // The Key is the Break for all individual breaks in the group
    var breaks = breakGroups.FirstOrDefault(x => x.Key == totalBreak.Break);

    // Do we have a match?
    if (breaks == null)
    {
        continue;
    }

    var breakLength = TimeSpan.Parse(totalBreak.Length);
    // Add up the individual breaks with Aggregate
    var breakTotal =
        breaks
            .Aggregate(
                TimeSpan.Zero,  // Initial break is 00:00:00
                (time, b) =>    // Add each break to the initial
                    time.Add(TimeSpan.Parse(b.Length)));

    // Does the break length match the total number of breaks?
    if (breakLength == breakTotal)
    {
    }
}

这是一个 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 功能):

var matching = commonlog.Where(
    l => groupedLogs.TryGetValue(l.Break, out TimeSpan v)
         && TimeSpan.Parse(l.Length) == v);