gpt4 book ai didi

c# - ArgumentOutOfRangeException 参数名称 : Index

转载 作者:太空宇宙 更新时间:2023-11-03 22:51:07 24 4
gpt4 key购买 nike

每当我尝试打开页面时,我总是收到此“需要 ArgumentOutOfRange 异常非负数,参数名称:索引”,但我似乎无法弄清楚负数出现的位置/方式。提前谢谢大家!!

var months = data.OrderBy(x => x.ApproximatedStartDate).Select(x => x.Month).Distinct((x, y) => x == y).OrderBy(x => x).ToList();
var upcomingMonths = months.GetRange(months.IndexOf(DateTime.Today.Month), months.Count - months.IndexOf(DateTime.Today.Month));

当代码读取“upcomingMonths”变量时出现异常。

堆栈跟踪:

[ArgumentOutOfRangeException: Non-negative number required. Parameter name: index]
System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) +72
System.Collections.Generic.List`1.GetRange(Int32 index, Int32 count) +4951591
InitializeChartBC()
Page_Load(Object sender, EventArgs e)
System.Web.UI.Control.OnLoad(EventArgs e) +103
System.Web.UI.Control.LoadRecursive() +68
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3811

最佳答案

根据列表的文档标题

// Exceptions:
// T:System.ArgumentOutOfRangeException:
// index is less than 0.-or-count is less than 0.

所以我认为 months 不包含当前月份。在调用月份之前。GetRange 检查它是否包含当前月份,然后调用 GetRange

var months = data.OrderBy(x => x.ApproximatedStartDate).Select(x => x.Month).Distinct((x, y) => x == y).OrderBy(x => x).ToList();
//Anyone corrent me as the list is converted to **.ToList** it wont throw null error I feel
List<T> upcomingMonths = null; //Where T is the type of the list
if(months.IndexOf(DateTime.Today.Month)>=0)
upcomingMonths = months.GetRange(months.IndexOf(DateTime.Today.Month), months.Count - months.IndexOf(DateTime.Today.Month));

关于c# - ArgumentOutOfRangeException 参数名称 : Index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47267963/

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