更新时间:2021-12-02 21:31:08
您的问题是:listDonnees [cpt]是一个List,您只需要获取List>的内部列表.如果您想知道内部列表是否包含 #FFFFFF ,则需要使用.Contains()
Your problem is that: listDonnees[cpt] is a List, you just take the inner list of you List>. If you want to know if the inner List contains the #FFFFFF you need to use .Contains()
listDonnees[cpt].Contains("#FFFFFF")
或者您对-循环使用第二个(嵌套的)循环来检查每个字符串. 顺便说一句:您可以使用以下方法更轻松地进行检查:
Or you use a second (nested) for - loop to check every string. By the way: You can check easier by using this:
colore = listDonnees[cpt].Any(s =>new List<String>() {"#FFFFFF", "#FFD700", "#FF6347"}.Contains(s));
所以对您来说尤其是问题:
So for you problem in particular:
int nbItems = listDonnees.Count ;
var hashCodes = new List<String>() {"#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)
{
<td style="background-color:@colorcode">colorcode</td>
//cpt++; See below!
}
else
{
var str = String.Join(",", listDonnees[cpt]);
<td>str</td>
}
// I Think your cpt++; needs to be here:
cpt++;
}
我不认识您的模型,所以您可能需要更改@ViewBag的访问权限.
I don't know you Model, so you might habe to change your @ViewBag accessing a bit.