Is this a lazy LINQ loading issue?

Something very strange is happening in my program:

I make this request agt.DefaultNr == 1 in the collection and get 3 elements as the result:

IEnumerable<Agent> favAgents =
            from agt in builtAgents where agt.DefaultNr == 1 select agt;

For each item, I set DefaultNr = 0

foreach (Agent noFavAgt in favAgents)
{
    noFavAgt.DefaultNr = 0;
}

I am making another request, but for some reason my favAgents collection is now empty!

IEnumerable<Agent> smallAgents = (from agt in favAgents
    where agt.tempResultCount < 30
    orderby agt.tempResultCount descending
    select agt);

What's going on here?

Is this a lazy LINQ loading issue?

It looks like there will be some kind of repeated request after I set all the elements = 0, because my collection is empty!

+5
source share
2 answers

, . , , . , ; , favAgents, , . , , ToList().

var favAgents = 
    (from agt in builtAgents where agt.DefaultNr == 1 select agt).ToList();

.

+11

, favAgents - "" , ! favAgents, . favAgents , .

, ToList - :

favAgents = favAgents.ToList();

- , . ToArray , .

+6

All Articles