获取“是”的计数从我的多个对象列表
本文关键字:对象 列表 string 我的 获取 | 更新日期: 2023-09-27 17:49:20
我想从列表(字符串)的多个对象中获得'yes'的计数。假设我有三个列表sig1
, sig2
和sig3
,它们都包含'yes'或'no'。我想从sig1
中得到'yes'的计数,其中sig2
是'yes', sig3
是'no'。
我从你的问题中得到的是,你可能有3个相同数量的字符串列表,你想知道yes
的计数,其中sign1
和sign2
是yes
, sign3
是no
。
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();