I have a collection of objects with properties and I want to remove all the trailing objects with (say) a value of 0 in LINQ.
public class Object
{
    public Object(){}
    public int Property {get; set;}
}
and if I have a list of objects:
new Object(){ Property = 1};
new Object(){ Property = 0};
new Object(){ Property = 9};
new Object(){ Property = 7};
new Object(){ Property = 0}; // "trailing zero"
new Object(){ Property = 0}; // "trailing zero"
new Object(){ Property = 0}; // "trailing zero"
How would I go about removing the "trailing zeros" in this list? I don't want to remove all properties with a zero, but I want to remove any objects from the list with a property value of zero if it it is not later followed by a property value of something greater.
Standard solution for sequences of finite size - reverse, remove from start, reverse:
   var withoutTail = sequence
       .Reverse()
       .SkipWhile( x => x == 0) // whatever condition you need
       .Reverse();
This is very non-optimal, so if you actually have real collection (i.e. List) it would be better to just remove items starting from last index.
Write an extension method:
static class Extensions
{
    public static IEnumerable<T> TrimTrailing<T>(this IEnumerable<T> items,
                                                 Predicate<T> test)
    {
        if (items == null) throw new ArgumentNullException(nameof(items));
        if (test == null) throw new ArgumentNullException(nameof(test));
        var buf = new List<T>();
        foreach (T item in items)
        {
            if (test(item))
            {
                buf.Add(item);
            }
            else
            {
                foreach (T bufferedItem in buf)
                {
                    yield return bufferedItem;
                }
                buf.Clear();
                yield return item;
            }
        }
    }
}
Then, if you have an IEnumerable<Object> called l, you would call TrimTrailing using
var trimmed = l.TrimTrailing(o => o.Property == 0);
Be careful with this, though: in the worst case, it buffers all the items from items and then throws away the buffer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With