繁体   English   中英

无法从列表中删除重复项<string>用不同的方法

[英]Cannot remove duplicates from a list<string> with Distinct method

我已经搜索了许多解决方案,但找不到任何对我有帮助的解决方案。

我从iWebElement收集数据,将其添加到List ,将其转换为string ,然后转换为List<string> ,这是我尝试执行的操作。 Distinct()没有结果。

var h1Heading = driver.FindElements(By.XPath("//h1"));
ListOfKeywords.AddRange(h1Heading);
foreach (IWebElement keywords in ListOfKeywords)
{
   cleaned.Add(keywords.Text);
}
cleaned.Distinct().ToList();
var mylist = cleaned.Distinct().ToList();

mylist 已经清理了不同的项目

为什么不通过这样做来简化它:

var h1Heading = driver.FindElements(By.XPath("//h1"));
ListOfKeywords.AddRange(h1Heading);
// Create a cleanup the list and assign it to the cleaned variable
cleaned = ListOfKeywords 
            .Select(l => l.Text) // No need for unessecary foreach
            .Distinct() // Create the distinct IEnumerable
            .ToList(); // Cast it to a new list

暂无
暂无

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

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