0

I've a listView that contains a collection of items that have the property IsFavourite, what I'm trying to do is set this property to false, for each items that was found in the Where clause.

What I did is this:

myListView.Items.Cast<Event>().Where(x => x.MatchLeague == leagueName && x.MatchNation == nationName).Select(c => { c.IsFavourite = false; return c; }).ToList();

so essentially I take all the items that have a certain leagueName and nationName, after did this, I tried to set for all items returned the IsFavourite property to false, and return the items as a list.

But I get this exception:

Operation is not valid due to the current state of the object

I've no idea what am I doing wrong, I even use this type of code in the past and all working well, what happen? Thanks.

Unchained
  • 187
  • 8
  • This may help you: http://stackoverflow.com/a/3921084 – Divyang Desai Sep 23 '16 at 11:22
  • Trying to modify an object like this is a very ugly abuse of LINQ. LINQ is a *query* language, not a data modification language. Simply *don't* use this hack, either return a new object or modify the items in a `foreach` loop – Panagiotis Kanavos Sep 23 '16 at 11:43
  • @PanagiotisKanavos also with a foreach loop I got this exception, that's why I asked here for solve this – Unchained Sep 23 '16 at 11:45
  • Post the *full* exception including the call stack, not just the message. This will show where the actual exception occured and any inner exceptions. You can get the full exception with a call to `Exception.ToString()`. In general you should log the entire exception – Panagiotis Kanavos Sep 23 '16 at 12:07
  • It would be very helpful if you posted the exception details, but from what you've posted my first guess would be that your query somehow modifies the myListView.Items collection (e.g. by binding, triggers, etc.), so as a quick-fix you could try caching the collection by using `myListView.Items.ToList()`. – Grx70 Oct 08 '16 at 21:34

2 Answers2

0

I am not sure what your complete stack trace is, if you are dealing with posts and are receiving max http collection key exceptions than you would need to add the below to your web config...

<appSettings>
    <add key="aspnet:MaxHttpCollectionKeys" value="10000" />
</appSettings>

However if this is related to Linq Queries alone, typically I see this kind of error when no matching records are found to alter IE: you are not finding any records with that leaguename and nationname combination.

0

The exception you are getting is because of invalid state of a run-time object, so the best option you have is to break up you statement like this, and check output of individual results, like so:

var r1 = myListView.Items.Cast<Event>();

var r2 = r1.Where(x => x.MatchLeague == leagueName && x.MatchNation == nationName);

var r3 = r2.Select(c => { c.IsFavourite = false; return c; });

var r4 = r3.ToList();

That might help you a bit.

You might also want to check this SO answer.

Community
  • 1
  • 1
Vijay Chavda
  • 826
  • 2
  • 15
  • 34