Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MonoTouch & LINQ - Operation is not valid due to the current state of the object

I have an application that uses Monotouch/C# on the Mac as the front end, pulling data from a WCF service in VS2010 on the backend. Everything was working like a dream last night before I left, and today, I'm not sure what's going on. All I can see is that it's an exception coming from within Monotouch's linq, and it looks like I'm doing everything OK, as I haven't touched this piece of code in the last few days.

After I get a GetAllStaffResult object back from WCF, I get myself back into the main thread and then call this method Populate():

public void Populate()
{
    foreach (var m in Staff)
    {
        m.FranchiseName = this.Franchises.Single(f => f.ID == m.FranchiseID).Name;
    }
    [snip]
}

Here's the stack trace:

System.InvalidOperationException: Operation is not valid due to the current state of the object.
at System.Linq.Enumerable.First[FranchiseListingItem] (IEnumerable'1 source, System.Func2 predicate, Fallback fallback) [0x0004a] in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System.Linq/Enumerable.cs:816
at System.Linq.Enumerbale.First [FranchiseListingItem] (IEnumerable'1 source, System.Func2 predicate) [0x00007] in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System.Linq/Enumerable.cs:843
at ADMobileServices.BOTransferDTOs.GetAllStaffResult.Populate() [0x0003b] (my code)

Not sure what could be causing this... anyone have any ideas? Going to see if I can find MT source files so I can see those lines.

Oh, this is a GetAllStaffResult, the method above is a partial class which has an array of staff members, franchises, locations. The populate method takes each staff member and populates the name of the franchise based on the staff's franchise ID. I'm doing this because I'm returning like 500 staff members with long franchise names and I'm saving bandwidth by reconstructing the object graph on the client side myself.

Because there is a foreign key relationship in the database, I'm 100% certain that Single will always return a result. First() gave me the same problem too.

EDIT: The stack trace is for the First() method which I tried after the Single() method. I'm checking out the source code now, will report my findings.

like image 368
Derreck Dean Avatar asked Feb 21 '23 18:02

Derreck Dean


1 Answers

This would happen if the sequence is empty.

like image 92
SLaks Avatar answered Apr 08 '23 16:04

SLaks