I have a list of items:
List<Item> ItemList = new List<Item>;
Sometimes the list is only partially full or certain indices are not occupied and therefore when I iterate through the list using a foreach, it gives an error because the item is null. I want to reduce that list to those items which actually have a value. This is what I'm trying:
var FullItems = ItemList.SkipWhile(Item => Item == null).ToList();
But when I check the FullItems list, it still contains the items from ItemList that are null, so I'm just getting back the entire list that I started with.
Help?
SkipWhile(i => i == null)
will skip until the first non-null item. Items after that first one which are null are still ignored.
Use Where(i => i != null)
to select all items that are not null.