2017-09-14 81 views
0

我有两个列表和类之间加起来时间C#匹配,然后在两个列表

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匹配的两个列表,然后添加了最后的数字0点00分30秒(3×30秒),并将总时间与总时间1进行比较,然后根据列表2中最后一个数字(时间)的总数是否等于列表1来作出决定。

我将如何实现那个?

+0

匹配在列表1和2的一切,或只为特定的价值? –

+2

为什么不作为int/TimeSpan存储而不是字符串? –

+1

列表1中是否有重复的第一个数字?如果是这样,你想比较列表2和他们每个人的总和吗? – juharr

回答

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特点):

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

非常感谢,这就像一个魅力 –

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