我是通用收藏的新手

我有一个班级。班级名称是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 +向上滚动鼠标)


我怎么解决这个问题 ?

最佳答案

我不完全确定整个逻辑是什么意思,但这就是我认为代码应为的样子:

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;


基本上,您new建立模型,设置属性,然后Add到模型列表;基于迭代子类别列表。

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

另外,可以进一步修剪此代码,但现在暂时不做介绍,以免与提供的代码相差太多。

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

10-08 23:36