System.Collections.Generic.List ‘不包含’equals’的定义

我有这个错误,我不明白为什么..

System.Collections.Generic.List’不包含’equals’的定义

我的列表是List <List >

  @{ int nbItems = ViewBag.listDonnees.Count ; bool colore ; for(int cpt = 0; cpt < nbItems; cpt++) { colore = false ; if(cpt + 1 < nbItems) { colore = @ViewBag.listDonnees[cpt].equals("#FFFFFF") || @ViewBag.listDonnees[cpt].equals("#FFD700") || @ViewBag.listDonnees[cpt].equals("#FF6347") ; } if(colore) { @ViewBag.listDonnees[cpt] cpt++; } else { @ViewBag.listDonnees[cpt] } } }   

我尝试使用“==”但我也有这个错误:

无法将运算符’==’应用于’System.Collections.Generic.List’和’string’类型的操作数

谢谢您的帮助

你的问题是:listDonnees [cpt]是一个List,你只需要列出你的List列表>。 如果你想知道内部List是否包含你需要使用的#FFFFFF .Contains()

 listDonnees[cpt].Contains("#FFFFFF") 

或者您使用第二个(嵌套)for循环来检查每个字符串。 顺便说一句:您可以使用以下方法更轻松地检查:

 colore = listDonnees[cpt].Any(s =>new List() {"#FFFFFF", "#FFD700", "#FF6347"}.Contains(s)); 

所以特别针对你的问题:

 int nbItems = listDonnees.Count ; var hashCodes = new List() {"#FFFFFF", "#FFD700", "#FF6347"}; for(int cpt = 0; cpt < nbItems; cpt++) { string colorcode = string.Empty; if(cpt + 1 < nbItems) { colorcode = listDonnees[cpt].FirstOrDefault(s => hashCodes.Contains(s)); } if(colorcode != string.Empty) { colorcode //cpt++; See below! } else { var str = String.Join(",", listDonnees[cpt]); str } // I Think your cpt++; needs to be here: cpt++; } 

我不认识你的模型,所以你可能会改变你的@ViewBag访问一点。