0

我想从我的多个 List(string) 对象中获取“ yes ”的计数。假设我有三个列表sig1sig2并且sig3它们都包含“”或“”。我想从“是”和“否的位置得到sig1是”计数。sig2sig3

4

3 回答 3

3

我从您的问题中得到的是,您可能有 3 个具有相同数量项目的字符串列表,并且您想知道yeswheresign1sign2isyessign3is的计数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 回答