我是通用集合的新手
我有一个班级。班级名称是ReportSubCategoryModel
这些是类属性
public class ReportSubCategoryModel
{
public string ReporTitle { get; set; }
public string ReporStatus { get; set; }
public string ReportDescription { get; set; }
public int ReporSubCategoryId { get; set; }
public IList<ReportSubCategoryModel> ReportSubCategoryModelList { get; set; }
}
我想从数据库中设置此类属性中的大量值。所以我分配了那个班级的名单
IList<ReportSubCategoryModel> reportSubCategoryModel = new List<ReportSubCategoryModel>();
现在我想在 for 循环中设置一个值
IList<ReportSubCategory> reportSubCategory = datamodel.ReportSubCategory.Where(r => r.ReportCategoryId == reportCategoryId).ToList();
for (int i = 0; i < reportSubCategory.Count; i++)
{
int reportSubCategoryId = reportSubCategory[i].ReportSubCategoryId;
ReportStatu reportStatus =
datamodel.ReportStatus.SingleOrDefault(
r => r.ReportSubCategoryId == reportSubCategoryId);
if (reportStatus == null)
{
reportSubCategoryModel[i].ReportDescription = "Dis";**//This line threw the error**
reportSubCategoryModel[i].ReporStatus = "Not Available";
reportSubCategoryModel[i].ReporTitle = reportSubCategory[i].ReportSubCategoryName;
reportSubCategoryModel[i].ReportSubCategoryModelList.Add(reportSubCategoryModel[i]);
}
else
{
reportSubCategoryModel[i].ReportDescription = "Dis";
reportSubCategoryModel[i].ReporStatus = "Available For " + reportStatus.ReportStatusDescription;
reportSubCategoryModel[i].ReporTitle = reportSubCategory[i].ReportSubCategoryName;
reportSubCategoryModel[i].ReporSubCategoryId = reportSubCategoryId;
reportSubCategoryModel[i].ReportSubCategoryModelList.Add(reportSubCategoryModel[i]);
}
}
return reportSubCategoryModel.ToList();
但它不起作用。
这一行reportSubCategoryModel[i].ReportDescription = "Dis";
给出了 Index 的错误超出范围。必须是非负数且小于集合的大小。
您可以从下图中看到这个问题和我的代码。请缩放您的浏览器(cntrl+向上鼠标滚动)
我怎么解决这个问题 ?