![](/img/trans.png)
[英]Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
[英]Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')"
我的 c# 控制台应用程序中有以下类:-
public class SearchCriteria
{
public string field { get; set; }
public string condition { get; set; }
public string value { get; set; }
public string logical_operator { get; set; }
}
public class ListInfo
{
public int row_count { get; set; }
public int start_index { get; set; }
public string sort_field { get; set; }
public string sort_order { get; set; }
public bool get_total_count { get; set; }
public List<SearchCriteria> search_criteria { get; set; }
}
public class Root
{
public ListInfo list_info { get; set; }
}
当我尝试填充根 object 时,如下所示:-
Root root = new Root(){};
root.list_info = new ListInfo();
root.list_info.row_count = 1000;
root.list_info.start_index = 1;
root.list_info.sort_field="subject";
root.list_info.sort_order = "asc";
root.list_info.get_total_count = true;
root.list_info.search_criteria = new List<SearchCriteria>();
root.list_info.search_criteria[0].field = "last_updated_time";
root.list_info.search_criteria[0].condition = "between";
root.list_info.search_criteria[0].value = "$(last_30_days)";
root.list_info.search_criteria[1].field = config.GetSection("ServiceDesk").GetSection("field").Value;
root.list_info.search_criteria[1].condition = "is";
root.list_info.search_criteria[1].value = "yes";
root.list_info.search_criteria[1].logical_operator = "AND";
我收到了这个错误:-
Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')"
在root.list_info.search_criteria[0].field = "last_updated_time";
..有什么建议吗?
您的 root.list_info.search_criteria仍然为空,因此此列表中没有项目。 你只应该做
root.list_info.search_criteria.Add(new SearchCriteria());
然后它将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.