【问题标题】:Looping through listbox items and triggering actions against each item循环遍历列表框项目并针对每个项目触发操作
【发布时间】:2019-12-04 10:15:55
【问题描述】:

我正在尝试遍历 ASP c# 列表框中的项目,在循环期间我打算从列表框中获取当前值并使用它从数据库中获取另一个值,然后使用数据库检索到的值来触发另一个操作(插入动态 CRM 数据库)。我收到错误:

集合已修改;枚举操作可能无法执行。

下面是我的代码,你知道我做错了什么吗?

foreach (object item in SelectedHobbies.Items)
{
    string hobby = SelectedHobbies.Items.ToString();
    string sql = "select cba_hobbyid from cba_hobby where cba_hobby = '" + hobby + "'";
    string hobbyId = Global_Class.GetSqlValue2(sql, "cba_hobbyid");

    try
    {
        using (OrganizationServiceProxy organizationServiceProxy = new OrganizationServiceProxy(this.OrganizationUri_prod, this.HomeRealmUri, this.Credentials, null))
        {
            IOrganizationService organizationService = organizationServiceProxy;
            // Entity contact = new Entity("contact");
            EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
            relatedEntities.Add(new EntityReference("contact", new Guid(hobbyId)));
            Relationship relationship = new Relationship("vrp_cba_hobby_contact");
            organizationService.Associate("contact", new Guid(contactId), relationship, relatedEntities);
            }

    }
    catch (Exception exception1)
    {
        Exception exception = exception1;
    }
}

【问题讨论】:

  • 您好!欢迎来到 SO。这能回答你的问题吗?stackoverflow.com/questions/1538817/…
  • 除了原始问题之外,我还发现了一些其他问题并希望解决,但是,此代码是foreach 块中的所有代码还是您省略了总和。因为我看不到错误可能出现在哪里
  • 这是'字符串爱好 = SelectedHobbies.Items.ToString();'正确还是应该是'string hobby = item.ToString();'?我不确定这是否与我问的错误有关。
  • 实际上,如果您修改SelectedHobbies.Items,则会出现此错误,但在您发布的代码中,我没有看到任何明确的指示。另一种可能性是您更改了底层数据绑定数据集合。您能否将代码发布到您用项目填充列表框的位置?
  • 方法:Associate是做什么的?

标签: c# asp.net listbox


【解决方案1】:

当枚举正在进行并且您再次触摸支持集合时,此异常很典型。

那么这发生在哪里以及如何发生:

// at this line you start enumerating the your collection
foreach (object item in SelectedHobbies.Items)
{
    // **** and here probably you are restarting the enumeration?
    // as pointed out the above statement is incorrect there is not enough
    // information point to the step that is changing the collection
    // backing the enumeration in the original post
    string hobby = SelectedHobbies.Items.ToString();
...

可能的解决方案是制作一个新的支持集合

foreach (object item in SelectedHobbies.Items.Cast<ListItem>().ToList()) // create a new list which has its own array backing the collecton
{
    string hobby = SelectedHobbies.Items.ToString();
...

【讨论】:

  • "// 这里可能您正在重新启动枚举?"这听起来像您在猜测......为什么ToString() 调用会重新启动枚举?你能备份你的论文吗?实际上这只会打印类名,例如“System.Windows.Forms.ListBox+ObjectCollection”(取自 Winforms 示例)
  • @MongZhu 抱歉,您检查了代码并且 listitemcollection 没有重新实现 ToString()。将更新答案。但它确实让我感到困惑是什么导致了异常。
  • @verbedr,感谢您的回复,我已经尝试过这个建议,但没有成功。 SelectHobbies 是一个列表框(Asp.net 控件)显然确实支持 ToList,请参见下面的错误:'ListItemCollection' 不包含“ToList”的定义,并且没有扩展方法“ToList”接受“ListItemCollection”类型的第一个参数可以找到(您是否缺少 using 指令或程序集引用?)
  • @NelsonKibet 我犯了一个错误,只是假设事情,应该在输入我的答案之前检查它。由于 ListItemCollection 是一个非通用集合,因此 ToList 不起作用,您首先需要强制转换(将更新答案)。但是在阅读了您的其他评论后,我认为这不会解决您的问题
【解决方案2】:

看起来像这个问题的重复:Collection was modified; enumeration operation may not execute

您的代码不同,但错误的根本原因是相同的。错误消息试图告诉您该集合在您循环时正在更改。

想象一下,在你玩了一副纸牌时,有人溜进来或拿新牌。

调用“ToList()”将从 IEnumerable 创建一个列表,如 Microsoft 文档中所述:https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.tolist?view=netframework-4.8

我认为你正在尝试做的是:

foreach (object item in SelectedHobbies.Items.ToList())
{
    string hobby = item.Value.ToString();
    string sql = "select cba_hobbyid from cba_hobby where cba_hobby = '" + hobby + "'";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2023-02-10
    相关资源
    最近更新 更多