gpt4 book ai didi

c# - MonoTouch 和 LINQ - 由于对象的当前状态,操作无效

转载 作者:太空狗 更新时间:2023-10-30 00:34:05 25 4
gpt4 key购买 nike

我有一个应用程序在 Mac 上使用 Monotouch/C# 作为前端,在后端从 VS2010 中的 WCF 服务中提取数据。昨晚我离开前一切都像梦一样进行,而今天,我不确定发生了什么。我所看到的是它是来自 Monotouch 的 linq 中的异常,看起来我做的一切都很好,因为我在过去几天没有触及这段代码。

在我从 WCF 得到一个 GetAllStaffResult 对象后,我让自己回到主线程,然后调用这个方法 Populate():

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

这是堆栈跟踪:

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)

不确定是什么原因造成的...有人有什么想法吗?看看我是否可以找到 MT 源文件,以便我可以看到这些行。

哦,this 是一个GetAllStaffResult,上面的方法是一个分部类,其中包含员工、特许经营权和位置的数组。 populate 方法获取每个员工并根据员工的特许经营 ID 填充特许经营名称。我这样做是因为我要返回大约 500 名具有长特许经营名称的员工,并且我通过自己在客户端重建对象图来节省带宽。

因为数据库中存在外键关系,我100%确定Single总会返回一个结果。 First() 也给了我同样的问题。

编辑:堆栈跟踪是针对我在 Single() 方法之后尝试的 First() 方法。我现在正在检查源代码,将报告我的发现。

最佳答案

如果序列为空,就会发生这种情况。

关于c# - MonoTouch 和 LINQ - 由于对象的当前状态,操作无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8930129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com