【问题标题】:c# Match between two lists, then add up time in between the two listsc# 两个列表之间的匹配,然后将两个列表之间的时间相加
【发布时间】:2017-09-14 16:44:11
【问题描述】:

我有两个列表和一个类

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 做出决定

我将如何实现这一目标?

【问题讨论】:

  • 匹配列表 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 中断中的所有购物车,并将列表 2 与购物车 0036 匹配的时间相加 此数据是从两个文本文件中读取的。然后存储在当前列表中,我愿意以不同的方式进行操作,谢谢

标签: c# list compare add


【解决方案1】:

这是一个 LINQ 解决方案,它以与 Romoku 答案类似(但更紧凑)的方式聚合您的 List 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);

【讨论】:

    【解决方案2】:

    您可以使用 GroupBy 对单个中断进行分组,然后循环通过聚合中断来查找匹配项。

    总结各个休息时间是Aggregate。

    对于Length,我建议使用TimeSpan 而不是string。

    数据

    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)
        {
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-16
      • 2015-05-23
      • 1970-01-01
      • 2019-01-27
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      相关资源
      最近更新 更多