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:

  1. Change ICollection to IList
  2. Use ElementAt that is inherited from IEnumerable. But be aware - it could not be efficient.
  3. 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++;
 }