1

I got a strange exception when trying to find out if a property in a list of objects is equal for all objects.

This is my code:

bool lvNoGuests = pvBillData.Reservering.Dagen.All(x =>
{
    return x.AantalKinderen == pvBillData.Reservering.Dagen[0].AantalKinderen &&
           x.AantalVolwassenen == pvBillData.Reservering.Dagen[0].AantalVolwassenen
});

The idea is to use the .All to see if all members of the list have the same value for the two properties as the first entry in the list.

The properties are simple getters (not auto property) and do not modify anything.

When I execute this code, I get an InvalidOperationException "Collection was modified; enumeration operation may not execute".

Anyone got a clue why?

The Lists are not genericht .net lists but are of the type XPCollection (XPO framework of DevExpress).

2
  • So you're not dealing with LINQ to Objects but with the DevExpress XPO LINQ implementation?
    – Steven
    Mar 8, 2010 at 16:21
  • I'm not sure about linq, but i think that it is ordinary linq-to-objects, since i dont query the database on the fly with linq, but i query the cached XPO obejcts. Which are internally some fancy lists with caching and reloading from DB.
    – Henri
    Mar 8, 2010 at 16:24

2 Answers 2

3

Is everything a simple getter? Even Dagen? I can only assume that sometihng funky is going on (perhaps with dynamic type creation and silent overrides that you don't see in you code - are the members virtual?). However, to fix it I would try grabbing the particulars into variables:

var first = pvBillData.Reservering.Dagen.First();
var kinderen = first.AantalKinderen;
var volwassenen = first.AantalVolwassenen;
bool lvNoGuests = pvBillData.Reservering.Dagen.All(
    x => x.AantalKinderen == kinderen && x.AantalVolwassenen == volwassenen);
7
  • Sorry Marc, I beat you on this one ;-)
    – Steven
    Mar 8, 2010 at 16:29
  • @Steven - indeed (+1), but I at least offered some thoughts on what might be happening, and simplified the lambda ;-p Mar 8, 2010 at 16:30
  • .Dagen had indeed a condition in the getter which caused the list to be reloaded from the DB. @Steven, sorry although you and Marc said the same, I'll give him the "answered" points since he was a little bit more explicit.
    – Henri
    Mar 8, 2010 at 16:32
  • 1
    D'oh! Van je Nederlandstalige broeders moet je het hebben :-)
    – Steven
    Mar 8, 2010 at 16:38
  • @Steven - "From your brothers need you speak Dutch"? mechanical translation still has a way to go, I suspect... Mar 8, 2010 at 17:38
3

Try this:

var eersteDag = pvBillData.Reservering.Dagen[0];
var verwachtAantalKinderen = eersteDag.AantalKinderen;
var verwachtAantalVolwassenen = eersteDag.AantalVolwassenen;

bool lvNoGuests = pvBillData.Reservering.Dagen.All(x =>
{
    return x.AantalKinderen == verwachtAantalKinderen &&
           x.AantalVolwassenen == verwachtAantalVolwassenen
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.