I'm trying to iterate over all the results from a linq statement. I understand that the linq statement is not executed until the foreach loop starts. The code steps through the foreach loop once with a result but when it tries to loop through a second time it throws the error "Object references not set to an instance of an object" on the object "SelectectedEntity".
List<FeedEntity> EntityList = feed.entity.ToList();
IEnumerable<FeedEntity> SelectectedEntity =
from entity in EntityList
where entity.trip_update.trip.trip_id == "9571620-BCC 17_18-BCC_FUL-M-Tu-W-Th-01"
select entity;
foreach (FeedEntity e in SelectectedEntity)
{
string s = e.trip_update.trip.route_id.ToString();
}
I'm expecting a few dozen elements in the SelectedEntity collection. What am I missing or have I misunderstood how linq operates.
The question is not what is a null reference, as I know what that is ..but why was I getting a null reference at this point in the code.
It had to do with the data and how linq interprets it. I'll post the correct code below as the answer.