2017-09-14 133 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) 
    { 
    } 
}