获取“是”的计数从我的多个对象列表

本文关键字:对象 列表 string 我的 获取 | 更新日期: 2023-09-27 17:49:20

我想从列表(字符串)的多个对象中获得'yes'的计数。假设我有三个列表sig1, sig2sig3,它们都包含'yes'或'no'。我想从sig1中得到'yes'的计数,其中sig2是'yes', sig3是'no'。

获取“是”的计数从我的多个对象列表<string>

我从你的问题中得到的是,你可能有3个相同数量的字符串列表,你想知道yes的计数,其中sign1sign2yes, sign3no

List<string> sign1, sign2, sign3;
int count = sign1.Where((item, index) => (sign1[index] == 'yes') &&
                                         (sign2[index] == 'yes') && 
                                         (sign3[index] == 'no')).Count();

你的问题真的很难理解,但我猜这样的东西是你想要的。

List<string> myAnswers = new List<string>() { "yes", "yes", "no" };
Int32 numberOfPositiveAnswers = myAnswers.Count(x => x.Equals("yes"));

numberOfPositiveAnswers的值为2。

所以,如果我理解正确的话,当列表2中相同索引处的项目为"yes"而列表3中相同索引处的项目为" no "时,您希望从列表1中获得"yes"的计数

    List<string> sig1 = new List<string>(new string[]{"yes","yes","yes"});
    List<string> sig2 = new List<string>(new string[] { "yes", "no", "yes" });
    List<string> sig3 = new List<string>(new string[] { "no", "yes", "no" });
    int count = sig1.Where((s, index) => s == "yes"
             && sig2.ElementAtOrDefault(index) == "yes" 
             && sig3.ElementAtOrDefault(index) == "no").Count();