我想从我的多个 List(string) 对象中获取“ yes ”的计数。假设我有三个列表sig1
,sig2
并且sig3
它们都包含“是”或“否”。我想从“是”和“否”的位置得到“sig1
是”的计数。sig2
sig3
问问题
105 次
3 回答
3
我从您的问题中得到的是,您可能有 3 个具有相同数量项目的字符串列表,并且您想知道yes
wheresign1
和sign2
isyes
和sign3
is的计数no
。
List<string> sign1, sign2, sign3;
int count = sign1.Where((item, index) => (sign1[index] == 'yes') &&
(sign2[index] == 'yes') &&
(sign3[index] == 'no')).Count();
于 2012-12-22T07:45:00.587 回答
1
你的问题真的很难理解,但我猜这就是你想要的。
List<string> myAnswers = new List<string>() { "yes", "yes", "no" };
Int32 numberOfPositiveAnswers = myAnswers.Count(x => x.Equals("yes"));
的值numberOfPositiveAnswers
将是 2。
于 2012-12-22T07:39:27.723 回答
0
因此,如果我理解正确,当列表 2 中相同索引处的项目为“是”且列表 3 的相同索引为否时,您希望从列表 1 中获取“是”计数。
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();
于 2012-12-22T07:45:40.967 回答