I have this:
List s = new List{\"\", \"a\", \"\", \"b\", \"\", \"c\"};
I want to remove all the empty elements
I write below code to remove the blank value
List<string> s = new List<string>{"", "a", "", "b", "", "c"};
s = s.Where(t => !string.IsNullOrWhiteSpace(t)).Distinct().ToList();
You can use List.RemoveAll:
C#
s.RemoveAll(str => String.IsNullOrEmpty(str));
VB.NET
s.RemoveAll(Function(str) String.IsNullOrEmpty(str))
Check out with List.RemoveAll with String.IsNullOrEmpty() method;
Indicates whether the specified string is null or an Empty string.
s.RemoveAll(str => string.IsNullOrEmpty(str));
Here is a DEMO.
s = s.Where(val => !string.IsNullOrEmpty(val)).ToList();