Remove items earlier than a fix date and replace the first occurrence

how to delete old calendar items in outlook 2016
javascript replace
remove character from string python
remove character from string javascript
how to delete calendar items in outlook web access
python remove character from string by index
str.replace python
javascript remove specific character from string

Based on a list of a typed class like

class CostCenter
{
    string center;
    string percentage;
    DateTime start;
}

I have a list of these elements like this:

  • {"a", 70, "2019/11/10"}
  • {"b", 30, "2019/11/10"}
  • {"A", 40, "2018/10/05"}
  • {"B", 60, "2018/10/05"}
  • {"xx", 10, "2010/01/01"}
  • {"yy", 90, "2010/01/01"}
  • {"100", 50, "2009/07/03"}
  • {"101", 50, "2009/07/03"}

taking into account that I want to get rid of all of the elements (list) that have a start date earlier or equal than a fixed(_earliestDate), also this fixed data will replace the earliest match. In this example the earliest date is "2010/11/01", therefore "xx" and "yy" start date is replaced to "2010/11/01". The output should be like this:

  • {"a", 70, "2019/11/10"}
  • {"b", 30, "2019/11/10"}
  • {"A", 40, "2018/10/05"}
  • {"B", 60, "2018/10/05"}
  • {"xx", 10, "2010/11/01"}
  • {"yy", 90, "2010/11/01"}

I have created the following code:

string dateToReplace = null;
bool earliestFound = false;
foreach (var item in list.OrderByDescending(x => DateTime.Parse(x.start)).ToList())
{
    if (earliestFound) //already marked, safe to remove others
    {
        list.Remove(item);
        continue;
    }

    if (!string.IsNullOrEmpty(dateToReplace)) //remaining matches
    {
        if (string.Equals(item.start, dateToReplace))
        {
            item.start = _earliestDate;
            continue;
        }
        else
        {
            earliestFound = true;
            list.Remove(item);
            continue;
        }
    }
    if (item.start <= _earliestDate) //first earliest or same, more required
    {
        dateToReplace = item.start;
        item.start = _earliestDate;
    }   
}

dateToReplace and earliestFound will be used as flags in the code to determine when to loop.

Somehow I think it is not the best option, do you have any suggestion on how to make it easier to understand or efficient?

To address the first part of your question : Removing items earlier that a given date.

Kiss is your best friend. either you reasign the list after a filtering or you remove all the item you don't want based on a predicate.

var inputs = new List<CostCenter> {
    new CostCenter("a", "70", "2019/11/10"),
    new CostCenter("b", "30", "2019/11/10"),
    new CostCenter("A", "40", "2018/10/05"),
    new CostCenter("B", "60", "2018/10/05"),
    new CostCenter("xx","10", "2010/01/01"),
    new CostCenter("yy", "90", "2010/01/01"),
    new CostCenter("100", "50", "2009/07/03"),
    new CostCenter("101", "50", "2009/07/03"),
};

var result1 = inputs.Where(x => x.start > DateTime.Parse("2010/01/01")).ToList();
var result2 = inputs.RemoveAll(x=> x.start <= DateTime.Parse("2010/01/01"));

Equipment maintenance, Requirements for selected calendar time change items are forecast quarterly in to maintenance supply liaison no later than three days following the "as of date. For FK supply items, the AFTO Form 349 will be provided no earlier than 60 to supply on the first workday of the month preceding the month the items are to  I want to delete the occurrence of a character in a string only for the first occurrence. Example: echo "B123_BACK" | tr -d 'B' This results in output: 123_ACK How can I delete only the first

First, you need to filter out all outdated items. Then, you can just take the first start, which is the earliest one, and then replace all with _earliestDate.

if (list == null)
    throw new ArgumentNullException(...);

// Get rid of unused items and sort:
var actual = list
    .Where(item => item.start >= _earliestDate)
    .OrderByDescending(item => item.start)
    .ToArray();

// If there are no matching items, then return empty array or throw exception
// It depends on your requirements
if (actual.Length == 0)
    return actual; 

// Take the least date in resulted array...
var earliestInActual = actual[actual.Length - 1]; 

// ...and replace it with _earliestDate
for (int i = actual.Length - 1; i >= 0; i--)
{
    if (actual[i].start != earliestInActual)
        break;

    item.start = _earliestDate;  
}

return actual;

Please, keep in mind that it will change original objects, therefore start dates will e updated in the original list as well.

Federal Register, AO 269—A 3] DOMESTIC DATES PRODUCED OR PACKED IN A after the second occurrence of the Word “production”, and change the period at the end of insert “prior to June 2 of the crop year” after the first occurrence of “certified”; delete Dates other than restricted dates may be disposed of in outlets prescribed  Finally, click either Replace to replace the found occurrences one by one, or Replace All to swap all the entries in one fell swoop. Tip. If something has gone wrong and you got the result different from what you'd expected, click the Undo button or press Ctrl + Z to restore the original values.

Removing items from a list while using a foreach loop will cause it to lose it's position, you can however use a for loop or just do another loop removing all the flagged ones.

DateTime cutOffDate = DateTime.Parse("2010/11/01");

List<CostCenter> removeList = new List<CostCenter>();

foreach (var item in list.OrderByDescending(x => x.start).ToList())
{
    if (item.start <= cutOffDate) {
        removeList.Add(item);
    }
}

foreach(var remove in removeList)
{
    if (list.Contains(remove)) {
        list.Remove(remove);
    }
}

New Frontiers in Applied Data Mining: PAKDD 2008 International , Besides, the last bit of AppearA is set to be 1. To eliminate the occurrence of the removed item “A”, the appearing bit se- quence of the maximum repeating It is implied that the first occurrence of “ABCB” is out-of-date because the first non-​zero bit in AppearABCB, located on bit 3, is less than the length of pattern “ABCB”​. The Excel REPLACE and SUBSTITUTE functions are very similar to each other in that both are designed to swap text strings. The differences between the two functions are as follows: SUBSTITUTE replaces one or more instances of a given character or a text string. So, if you know the text to be replaced, use the Excel SUBSTITUTE function.

We can create new filtered list with given criteria

        DateTime threshold = new DateTime(2010, 11, 01);
        List<CostCenter> result = cc.Where(x => x.start.Date > threshold.Date).ToList();

POC : .netFiddle

InfoWorld, For instance, if the end date of the first task in one project changes, then the start date change lets you remove any task shifts that were imposed by previous Even so, Project 3.0 is still fast compared with many Windows and DOS products. To address the first part of your question : Removing items earlier that a given date. Kiss is your best friend. either you reasign the list after a filtering or you remove all the item you don't want based on a predicate.

For a List where 99% of the elements has to be removed, you will currently iterate throught the whole list to find all those elments and removing them. Instead of clearing the List select only the part you wan't and stop as soon as you have Date inferior to earliestDate. With a "TakeWhile" approach just add a flag to know when to edit the element to fix the date.

public static IEnumerable<CostCenter> TakeWhileAndOneAfter
    (IEnumerable<CostCenter> source, DateTime splitDate )
{
    DateTime? earliestDate = null;
    bool earliestFound = false;

    foreach (CostCenter element in source)
    {
        if (!earliestFound && !(element.start > splitDate))
        {
            earliestFound = true;
            earliestDate = element.start;
        }

        if (earliestFound)
        {
            if (element.start < earliestDate)
            {
                break;
            }
            else
            {
                element.start = splitDate;
            }
        }
        yield return element;
    }
}

Minutes of Evidence Taken Before Her Majesty's Commissioners , The various elements of reduction consisted , first , about 1876 . in the invention of and other machinery , but the self - binder removed the only limit to production . s improvements ; and the Dakotas , which at about that date for the first time For instance , the greatest change since ihen in the cost of transportation had  Java String replace() Method example. In the following example we are have a string str and we are demonstrating the use of replace() method using the String str. We have replaced all the occurrences of char ‘o’ with char ‘p’.

Management Engineering: The Journal of Production, In fact , the experienced workman will usually know all the elements if the group is properly named . For instance , under the first operation on the sample instruction sheet 0 . Change card at window . Remove cutter from holder . Drawn by Date End a End b | Checked by Date Revised by Late - - 6 SZEK - - - - - - Fig . Replace First or Last Occurrence of a String with C# First Published 5th February 2010, Last Updated 7th January 2019 by Tim Trott This snippet provides two functions to replace only the first or the last occurrence of a string within a larger string.

The Academy and Literature, Skeat had really succeeded in accounting for the order of the first thirteen runes their first occurrence as initials in the English Paternoster, discovered that this Let L and T change places, and let B move six steps back ; and you have as before by first arranging tho letters according to tho Latin order, and then removing  search and replace the last occurance of a match in a file HI please let me know if there is any command to search and replace only the last occurence of a string in aline. Eg: " This cat is a cat with a tail longer of all cat."

str_replace - Manual, str_replace — Replace all occurrences of the search string with the replacement string If search or replace are arrays, their elements are processed first to last. If passed, this will be set to the number of replacements performed. If you want to remove all dashes but one from the string '-aaa----b-c-----d--e---f' resulting in  i have a file called file1 cat file1 i am namish namish lives in India India and namish both are good. I want to delete all the occurences of namish in one shot,if i do it with sed i guess all the lines will be deleted containing the pattern.Suggest me any idea without AWK.

Comments
  • Does your code works without exception? Usually, it is not allowed to alter a list, while iterating over it at the same time... Regarding your question: a different approach may be the linq Where() method to only fetch the items from the list that should be removed or updated.
  • Wow, it looks like a very complicated way to do something very simple. At the same time, I didn't understand what you are trying to achieve.
  • @YeldarKurmangaliyev this seem to be a cleanup of a history alike list
  • @DanielV, Instead of removing items from list, can we create new list with filtered results? If Yes then we can do it in one line
  • @PrasadTelkikar yes, there is no restriction to remove items from the list
  • I may be wrong, but this solution seems to be missing "also this fixed data will replace the earliest match" part.
  • @YeldarKurmangaliyev, I din't get this part, if row 100 and 101 disapears without fixing the date. Why are the date of xx and yy changed.
  • @xdtTransform your solution doesn't replace "xx" and "yy" with the start date to "2010/11/01"
  • Not sure what you mean, sorry
  • @DanielV, My bad I edited a new answer into this post then edited it out and post it as an other answer but system blocked my new answer. or ajax error .. I don't know but the new code was lost in the process. going thought edit history I got it back and push it as a new answer again .
  • I think this is the closest to an answer out of all the ones posted, item.start > _earliestDate I think will remove too many results op wants to keep {"yy", 90, "2010/01/01"}
  • @Matt I have updated the answer with >=, thanks :)
  • Not sure I'm following, how will you lose data?
  • Your solution is not replacing the earliest match with the cutOffDate, please take a look at the requirement, I have updated the description to make it more clear.