0

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();
1
  • 6
    Distinct() returns an ienumerable with no duplicates. you have to assign it to a variable, though, it does not change the original in place - try cleaned = cleaned.Distinct().ToList() Commented Feb 29, 2020 at 10:09

2 Answers 2

2
var mylist = cleaned.Distinct().ToList();

mylist has cleaned items after distinct

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.