Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.ICollection in mvc controller

前端 未结 3 800
悲哀的现实
悲哀的现实 2021-02-19 00:10
public ActionResult addstandardpackage1(ICollection SingleStay,ICollection DOUBLESTAY,ICollection TRIBLESTAY,ICollection FAMI         


        
相关标签:
3条回答
  • 2021-02-19 00:28

    or you can use

    foreach (var item in Collection)
    {
    ..................
    }
    
    0 讨论(0)
  • 2021-02-19 00:30

    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++;
     }
    
    0 讨论(0)
  • 2021-02-19 00:39

    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.

    0 讨论(0)
提交回复
热议问题