将For循环转换为Linq

将For循环转换为Linq,linq,Linq,下面的代码按预期工作,但我想知道是否有办法使用Linq改进我的代码 我所做的是查找第[5]行和第[6]行是否有值 for (int i = 0; i < nameList.Count; i++) { IList<IWebElement> rows = driver.FindElements(By.CssSelector("itd_1")); for (int k = 0; k < rows.Count; k++) { if (

下面的代码按预期工作,但我想知道是否有办法使用Linq改进我的代码

我所做的是查找第[5]行和第[6]行是否有值

for (int i = 0; i < nameList.Count; i++)
{
     IList<IWebElement> rows = driver.FindElements(By.CssSelector("itd_1"));
     for (int k = 0; k < rows.Count; k++)
     {
        if (rows[5].Text != " " && rows[6].Text != " ")
        {
           if (!string.IsNullOrEmpty(rows[5].Text) || 
               !string.IsNullOrWhiteSpace(rows[5].Text) && 
               !string.IsNullOrEmpty(rows[6].Text) || 
               !string.IsNullOrWhiteSpace(rows[6].Text))
           {
              //do something here...
           }
        }
    }
 }
for(int i=0;i
尝试上述方法

   rows.Where(x => x[5].Text != " " && x[6].Text  != " ")
            .Where(x=> !string.IsNullOrEmpty(x[5].Text ) || !string.IsNullOrWhiteSpace(x[5].Text )
                && !string.IsNullOrEmpty(x[6].Text ) || !string.IsNullOrWhiteSpace(x[6].Text ));

这将为您提供所需结果的
IEnumerable

这将完全满足您上面的要求,只需删除冗余部分。假设
//在此处执行操作..
块没有更改行值

for (int i = 0; i < nameList.Count; i++)
{
     IList<IWebElement> rows = driver.FindElements(By.CssSelector("itd_1"));
     if (!string.IsNullOrWhiteSpace(rows[5]) && !string.IsNullOrWhiteSpace(rows[6]))
     {
        for (int k = 0; k < rows.Count; k++)
        {
              //do something here...
        }
    }
}
for(int i=0;i
外部循环似乎对数据没有任何作用?你只是在做这个
名字列表。数数
次?好像少了什么东西。。。您的意思是通过
i
获取选择器吗<代码>“itd”+i
也许?事实上,您的内部循环(
k
)也没有做任何有建设性的事情。我认为这个例子是错误的。如果代码是无意义的,我们无法帮助您。如果您不使用
k
并直接通过索引访问
行,那么
for
循环的作用是什么?您的代码非常奇怪。您不使用
i
k
任何地方,第一个for循环是100行的大列表,第二个for循环是11的计数,因为我知道我需要什么项目数据,所以您会看到硬编码值5和6。。。。我的代码可能没有意义,但它的工作方式应该是。。。你们知道Selenium API的人都能理解我在做什么。@thedailwtf:我在这里发布转换为linq的原因,你们明白了吗?newStackExchangeInstance你们是对的!!这是一个快速的解决方案:)除了IsNullOrWhitespace之外,所有这些检查都是无关的。string.Trim().IsNullOrEmpty()=string.IsNullOrWhitespace(),同意新闻
for (int i = 0; i < nameList.Count; i++)
{
     IList<IWebElement> rows = driver.FindElements(By.CssSelector("itd_1"));
     if (!string.IsNullOrWhiteSpace(rows[5]) && !string.IsNullOrWhiteSpace(rows[6]))
     {
        for (int k = 0; k < rows.Count; k++)
        {
              //do something here...
        }
    }
}