【发布时间】: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