0

I have id's like follows:

id='vat-code-A'

id='vat-code-B'

id='vat-code-c'

id='vat-code-D'

I want to check if the above element(s) count>0, which can be done using a for each loop. But, is there a way to perform the above requirement with Linq as below.

 var codes = new List<string>() { "A", "B", "C", "D" };
 var vatElement = Driver.FindElements(By.Id($'vat-code-{codes .Select(x=>x)}"));                    
        if (((vatCodeElement.Count()) > 0) == true)
        {                
            return true;
        }
2
  • Please show us your existing working code. Commented Jan 22, 2019 at 11:54
  • 1
    dotnetfiddle.net/nZkoi5 ... By.Id("id='vat-code-System.Linq.Enumerable+WhereSelectListIterator``2[System.String,System.String]") doesn't make much sens Commented Jan 22, 2019 at 11:58

1 Answer 1

1

You can use :

var codes = new List<string>() { "A", "B", "C", "D" };
var elements = codes.Select(c => driver.FindElements(By.Id("vat-code-" + c))).ToList();
if (elements.Select(x=>x.Count).Sum()>0)
 {
      // return true    
 }

or you can use SelectMany :

var elementsCount = elements.SelectMany(x => x).Count();
if (elementsCount > 0)
{
   // return true             
} 

if you want to all elements be present you can use :

var codes = new List<string>() { "A", "B", "C", "D" };
var elements = codes.Select(c => driver.FindElements(By.Id("vat-code-" + c))).ToList();
if (elements.Where(x => x.Count > 0).Count()==4)
 {
      // return true    
 }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer @Mehrdad. Correct me if I am wrong! If one of the elements is missing, this will still pass, I think. I should have asked my question with clarity. If I change the above code to if (elements.Select(x=>x.Count).Sum()>4) { // return true } Will it confirm the presence of all the four elements?
"If one of the elements is missing, this will still pass?" Yes
you want to all elements be present?
Yes. I should have mentioned it. Sorry

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.