简体   繁体   English

循环遍历列表并将前一行与当前行进行比较。 我有一个帐号列表,其中大部分是重复的

[英]Loop through list and compare previous line to current line. I have a list of account numbers mostly duplicates

My result two is a list of account numbers and dates that I have added from another list by using LINQ to sort.我的结果二是我通过使用 LINQ 排序从另一个列表中添加的帐号和日期列表。 The list of result two contains many duplicate account numbers but I would like to filter the list to remove duplicates but not just selecting the first instance.结果二的列表包含许多重复的帐号,但我想过滤列表以删除重复项,而不仅仅是选择第一个实例。

I am looking to select the account number that has the oldest date.我希望选择日期最早的帐号。 I'm getting some output from the stream reader but its not behaving as expected.我从流阅读器获得了一些输出,但它的行为不符合预期。 I am still getting every line and no removals.我仍然得到每一行并且没有删除。

I'm trying to compare if the account number matches the account number from the previous line and the current date is older to remove the instance of the account number with the newer date.我正在尝试比较帐号是否与上一行的帐号匹配,并且当前日期是否较旧,以删除具有较新日期的帐号实例。

Any ideas?有任何想法吗?

    foreach (var valueTwo in resultTwo)
    {
        var valueString = valueTwo.Name.ToString();
        var currentAccount = valueString.Substring(0, valueString.IndexOf('\t'));
        var currentDateTwo = (valueString.Substring((valueString.IndexOf('\t') + 1), 10).Replace("/", ""));
        int currentDate = Convert.ToInt32(currentDateTwo);
        var prevAccount = "";
        int prevDate = 0;

        if (currentAccount != prevAccount)
        {
            forecastTestList.Add(currentAccount + '\t' + currentDate);
        }

        if (currentAccount == prevAccount)
        {
            if (currentDate > prevDate)
            {
                forecastTestList.Remove(prevAccount + '\t' + prevDate);
                forecastTestList.Add(currentAccount + '\t' + currentDate);
            }
        }
        prevAccount = currentAccount;
        prevDate = currentDate;

        //srcheckForecast.WriteLine(valueTwo.Name);
    }

    foreach (var valueThree in forecastTestList)
    {
        srcheckForecast.WriteLine(valueThree);
    }
}

An alternative approach would be to use Dictionary data structure to store the maximum day for each account:另一种方法是使用Dictionary数据结构来存储每个帐户的最大天数:

var dictionary = new Dictionary<string, int>();

foreach (var valueTwo in resultTwo)
{
    var valueString = valueTwo.Name.ToString();
    var currentAccount = valueString.Substring(0, valueString.IndexOf('\t'));
    var currentDateTwo = (valueString.Substring((valueString.IndexOf('\t') + 1), 10).Replace("/", ""));
    int currentDate = Convert.ToInt32(currentDateTwo);

    if (!dictionary.TryGetValue(currentAccount, out int value) || currentDate > value)
    {
        dictionary[currentAccount] = currentDate;
    }   
}

// transorm dictionary to List<string> of your form
var forecastTestList = dictionary.Select(x => $"{x.Key + '\t' + x.Value}").ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM