gpt4 book ai didi

c# - 索引超出范围。必须是非负数且小于集合的大小

转载 作者:太空宇宙 更新时间:2023-11-03 16:12:44 27 4
gpt4 key购买 nike

我是通用集合的新手

我有一个类。类名是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"; 给出了索引 超出范围的错误。必须为非负数且小于集合的大小。

你可以从下图中看到这个问题和我的代码。请缩放你的浏览器(cntrl+向上鼠标滚动) enter image description here

我该如何解决这个问题?

最佳答案

我不完全确定整个逻辑是什么意思,但我觉得代码应该是这样的:

IList<ReportSubCategory> reportSubCategory = datamodel.ReportSubCategory
.Where(r => r.ReportCategoryId == reportCategoryId)
.ToList();

foreach (var subCategory in reportSubCategory)
{
int reportSubCategoryId = subCategory.ReportSubCategoryId;

ReportStatus reportStatus = datamodel.ReportStatus
.SingleOrDefault(r => r.ReportSubCategoryId == reportSubCategoryId);

if (reportStatus == null)
{
var model = new ReportSubCategoryModel();

model.ReportDescription = "Dis";
model.ReporStatus = "Not Available";
model.ReporTitle = subCategory.ReportSubCategoryName;

// Not sure what this is.
//model.ReportSubCategoryModelList.Add(reportSubCategoryModel[i]);

reportSubCategoryModel.Add(model);
}
else
{
var model = new ReportSubCategoryModel();

model.ReportDescription = "Dis";
model.ReporStatus = "Available For " + reportStatus.ReportStatusDescription;
model.ReporTitle = subCategory.ReportSubCategoryName;
model.ReporSubCategoryId = reportSubCategoryId;

// Not sure what this is either.
//reportSubCategoryModel[i].ReportSubCategoryModelList.Add(reportSubCategoryModel[i]);

reportSubCategoryModel.Add(model);
}
}

return reportSubCategoryModel;

基本上,您新建一个模型,设置属性,然后将其添加到模型列表;基于迭代子类别列表。

但是,我不确定整个嵌套列表是关于什么的(我注释掉的代码)。

此外,此代码还可以进一步精简,但我暂时不考虑它,以免与提供的代码有太大偏差。

关于c# - 索引超出范围。必须是非负数且小于集合的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16895089/

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