Home c# check if list contains an existing element (not null)
Reply: 2

c# check if list contains an existing element (not null)

Mc Midas
Mc Midas Published in 2018-02-12 16:12:13Z

Is there some equivalent to myTestList.Count that will only count not-nullable fields?

For example I want to do certain things when I know that between some null elements is one existing element.

This is the behaviour I want, but can this be achieved also with pre-existing functions?

if(myList.Count > 0){
    for(int i = 0; i < myList.Count; i++){
        if(myList[i] != null){
David Arno
David Arno Reply to 2018-02-12 16:15:54Z

Use the linq Any method:

if (myList.Any(i => i != null))
Kallen Newick
Kallen Newick Reply to 2018-02-12 16:16:29Z

If you aren't fussed about checking a specific index of the list not being null and just want to check there is something in the list you could use this.

if(myList != null && myList.Any())
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304499 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO