简体   繁体   中英

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

I already searched many solutions and couldn't find any that helped me.

I gather data from a iWebElement , add it to a List , convert it to string and later to a List<string> that's when I try to do . Distinct() resulting on nothing.

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 已经清理了不同的项目

Why not simplify this by doing this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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