【问题标题】:Foreach statement cannot operate on variables of type 'Table1' because 'Table1' does not contain a public instance definition for 'GetEnumerator'Foreach 语句无法对“Table1”类型的变量进行操作,因为“Table1”不包含“GetEnumerator”的公共实例定义
【发布时间】:2020-10-21 12:47:27
【问题描述】:

我收到此错误, Table1 是“表”中的外键, 我该如何解决这个错误?

Foreach 语句无法对“Table1”类型的变量进行操作,因为“Table1”不包含“GetEnumerator”的公共实例定义

       @if (Model != null)
                                {
                            @foreach (var items in Model)
                            {
                            @foreach (var aa in items.Table)
                            {
                               
                                @foreach ( var bb in aa.Table1)  // I have error in this object
                                {
    
                                    @foreach (var ee in bb.Table2)
                                    {
    }
}
}
}

表 1 模块

using System;
using System.Collections.Generic;
namespace website.Models
{
    public partial class Table1
    {
        public Table1()
        {
            Table= new HashSet<Table>();
          
        }

        public int Id { get; set; }
        public int? Number { get; set; }
        public int CountryId { get; set; }
        public string Name { get; set; }
        public string ResponsibleName { get; set; }  
       

        public Table2 Table2{ get; set; }
       
    }
}

表模型

using System;
using System.Collections.Generic;

namespace website.Models
{
    public partial class Table
    {
        public Table()
           
        {
            Projects = new HashSet<Projects>();
           

        }

        public int Id { get; set; }
        public int? ItemId { get; set; }
        public int? ExternalId { get; set; }
        public decimal? Cost { get; set; } 


        public Table1 Table1{ get; set; }
       
    }
}

【问题讨论】:

  • 报错信息很清楚...开始阅读official documentation
  • 您的Table1 甚至无法按原样编译(Table 是从哪里来的?)。
  • 带有类似Table2 的变量的问题非常难以可视化和协助。
  • @mjwills 我加入了四个表以从 _table2 获取数据,我还将更新 _table 模型
  • @MohamedAbubakkar, foreach 用于遍历集合中的项目,例如表格的行。但是您提供的是表格本身。

标签: c# asp.net linq asp.net-core .net-core


【解决方案1】:

foreach 仅适用于可枚举序列。这意味着foreach 中的类应该实现IEnumerable。错误是你的类Table1没有实现这个接口。

问题是:Table1 代表一个序列吗?换句话说:如果人们订购Table1 的对象:“给我你的第一个元素。然后给我下一个”,你能给出一个合乎逻辑的答案吗?

如果我查看您的Table1,在我看来它在字段或属性Table 中包含一个私有HashSet。在您的示例代码中,您忘记定义Table

我猜如果人们希望将Table1 视为一个序列,他们希望看到存储在 HashSet 中的元素。因此,如果他们要求 Table1 表示的序列的第一个元素,他们期望 HashSet 的第一个元素。

这意味着如果有人请求Table1 的第一个元素,他们会得到一个Table 对象。 Table1Tables 的序列。

你很幸运:HashSet&lt;Table&gt; 也实现了IEnumerable&lt;Table&gt;,所以如果有人要求 Enumerator,你可以简单地将请求转发到 HashSet。 按如下方式更改您的班级:

public partial class Table1 : IEnumerable<Table>, IEnumerable
{
    private HashSet<Table> Table {get;} = new HashSet<Table>();

    ...

    public IEnumerator<Table> GetEnumerator()
    {
        return this.Table;
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}

有时,您不想简单地返回另一个序列,您想逐个计算元素。在这种情况下,最简单的解决方案是使用yield return

class Table1 : IEnumerable<MyClass>
{
    private HashSet<Table> myHashSet = ...

    public IEnumerator<MyClass> GetEnumerator()
    {
        foreach (Table table in this.myHashSet)
        {
            MyClass myObject = ConvertToMyClass(table);
            yield return myObject;
        }
    }

    private MyClass ConvertToMyClass(Table table) {...}
}

'yield return' 是一种创建枚举器的简单方法。在上面的示例中,我将 HashSet 的每个元素都转换为 MyClass 对象,并yield returned 它。这意味着GetEnumerator 返回一个IEnumerator&lt;MyClass&gt;,从而实现IEnumerable&lt;MyClass&gt;

收益回报的另一个例子:

string a = ...
string b = ...
string c = ...

public IEnumerator<string> GetEnumerator()
{
    yield return a;
    yield return b;
    yield return c;
}

事实上,使用 yield return 你可以返回你想要的每一个元素,只要它可以被转换成你的枚举器的类型。每个收益返回都返回序列的下一个元素:

    yield return 1;
    yield return 1;
    yield return 5;
    yield return 2;
    yield return 2;
    yield return 2;

这表示int [1, 1, 5, 2, 2, 2]上的序列

【讨论】:

  • "这意味着 foreach 中的类应该实现 IEnumerable。"那是错误的。你不需要实现IEnumerable 接口,你只需要一个公共的无参数GetEnumerator 方法,它返回类似于IEnumerator 的东西。那是c#编译器依赖duck typing.的地方之一
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-08
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多