[英]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.