无法将[]的索引应用于类型'System.Collections.Generic.ICollection< int>在MVC控制器中 [英] Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.ICollection<int> in mvc controller

查看:397
本文介绍了无法将[]的索引应用于类型'System.Collections.Generic.ICollection< int>在MVC控制器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public ActionResult addstandardpackage1(ICollection<int> SingleStay,ICollection<int> DOUBLESTAY,ICollection<int> TRIBLESTAY,ICollection<int> FAMILYSTAY,ICollection<int> EXTRABED)
{
    var s = SingleStay;
    for (int i = 0; i < SingleStay.Count; i++ )
    {
        var cal = SingleStay[i];
    }
    foreach (var key in SingleStay)
    {
        var value = key;
    }          

}

在for循环中,我收到类似无法对[]的类型的表达式应用索引的错误,但是我需要在for循环中,每获得一次我就需要一次.因为基于for循环,我会将详细信息与其他收集列表绑定在一起.请帮助我.

In for Loop i am get the error like Cannot apply indexing with [] to an expression of type But i need in for loop , in for each i am getting . because based on for loop i will bind the details with other collection lists. Please Help me.

我在var cal=Singlestay[i]中遇到错误.

推荐答案

只需将其转换为数组:

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++;
 }

这篇关于无法将[]的索引应用于类型'System.Collections.Generic.ICollection&lt; int&gt;在MVC控制器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆