![](/img/trans.png)
[英]System.ArgumentOutOfRangeException: Index was out of range
[英]LINQ | ArgumentOutOfRangeException: Index was out of range
for (int i = 0; i < intFeatureIDs.Count; i++) {
slots_for = slots_for.Where(s => s.featureSlotMapping.Any(fsm => fsm.featureID == intFeatureIDs[i]));
}
在這里,intFeatureID.Count是2。並在異常下面拋出。
Message =嘗試評估LINQ查詢參數表達式時引發了異常。 若要顯示其他信息,請在重寫DbContext.OnConfiguring時調用EnableSensitiveDataLogging()。
內部異常1:ArgumentOutOfRangeException:索引超出范圍。 必須為非負數並且小於集合的大小。
如果intFeatureIDs.Count == 1,則沒有問題。
另外,如果我注釋掉for循環,並手動運行兩次代碼,它將起作用...
slots = slots.Where(s => s.featureSlotMapping.Any(fsm => fsm.featureID == intFeatureIDs[1]));
slots = slots.Where(s => s.featureSlotMapping.Any(fsm => fsm.featureID == intFeatureIDs[2]));
任何意見,不勝感激。
我建議將代碼更改為:
for (int i = 0; i < intFeatureIDs.Count; i++) {
var bob = intFeatureIDs[i];
slots_for = slots_for.Where(s => s.featureSlotMapping.Any(fsm => fsm.featureID == bob));
}
這解決了代碼中修改后的閉包問題,並可能使您的代碼與可能更改/重新分配intFeatureIDs
其他代碼(示例中未顯示) intFeatureIDs
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.