繁体   English   中英

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

[英]get count of “yes” from my multiple objects of List<string>

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

我从您的问题中得到的是,您可能有3个具有相等项数的字符串列表,并且您想知道yes的计数,其中sign1sign2yessign3no

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中相同索引处的项目为“是”并且列表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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM