MyList = (List)Session["MyList "]; if(MyList !=null ||...">

Checklist line is null or empty

I have a list with empty space ("__")

List<string> MyList = (List<string>)Session["MyList "]; if(MyList !=null || MyList != "") { } 

MyList! = "" Does not work if the line has more space, therefore

How can I check if my list string is "" or null using linq in C #?

+6
source share
3 answers
 if(MyList!=null || MyList.All(x=>string.IsNullOrWhiteSpace(x))) { } 
+16
source

Try the following:

 if(MyList.All(s=>string.IsNullOrWhiteSpace(s))) { .... } 
+4
source
 var emptyStrings = MyList.Where(p => string.IsNullOrWhiteSpace(p)).ToList(); var listWithoutEmptyStrings = MyList.Where(p => string.IsNullOrWhiteSpace(p)).ToList(); 

If you just want to check if the list contains one or more of these elements:

 if (MyList.Any(p => string.IsNullOrWhiteSpace(p))) { } 

If you want to check if all elements are empty or empty

 if (MyList.All(p => string.IsNullOrWhiteSpace(p))) { } 
+1
source

Source: https://habr.com/ru/post/955867/


All Articles