If collection is empty then write line

Hi all,

Small question: I have a collection and I am trying to check whether the collection is empty.
The name of the collection is “consultants” and is a list of strings.
I tried as followed but then I get an error that the sequence does not contain elements:
image

Thanks

2 Likes

use Consultants.count>0(this will return you true if your collection is non empty)

7 Likes

Hi there @yannip,
To check whether a collection is null or not populated, you can use the following:

If IsNothing(Consultants) ORELSE Consultants.Count < 1 Then

  • Do your stuff

Else

  • Do your other stuff

End If

If, however, you wanted to check whether the collection is null, not populated, or contains only empty values, you can use:

If IsNothing(Consultants) ORELSE Consultants.Count < 1 ORELSE Consultants.AsEnumerable.All(Function (strConsultants) String.IsNullOrWhiteSpace(strConsultants)) Then

  • Do Stuff

Else

  • Do Other Stuff

End If

Thanks in advance,
Josh

6 Likes

thanks @divyashreem and @Mr_JDavey for the quick help. Was indeed what I was looking for.

2 Likes