Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.ICollection<int> in mvc controller
ICollection
doesn't expose indexer
. You have three options:
- Change
ICollection
toIList
- Use
ElementAt
that is inherited fromIEnumerable
. But be aware - it could not be efficient. - Evalute passed collection to list (
ToList()
)
ICollection (and its exposed methods) on msdn.
Just convert it to an array:
var s = SingleStay.ToArray();
note that this will consume additional memory though.
Better way would be to get an Array or any other collection-form that supports indexer in the first place.
Yet another way would be to implement it with an index variable:
var s = SingleStay;
int i = 0;
foreach (var cal in s)
{
//do your stuff (Note: if you use 'continue;' here increment i before)
i++;
}