DotNet Mirror
  DNM facebook   DNM Google+   DNM Twitter   

Remove null/blank values from an array using C#.NET

By Ashok Nalam on 25 Feb 2013 | Category: C# | Tagged: array 
In this snippet we will see how to Remove null/blank values from an array using c#.NET.
  Discuss Add Comment   |  
Add rating Rate this resource   
Sometimes when we are fetching values from database into an array we might get blank/null values also. In order to remove the blank/null values,use the below code.

Sample array having blank and null values:

string[] myColors = { "Red", "Blue", "", "Green", "", null, "pink" };

using LINQ:

myColors = myColors.Where(color => !string.IsNullOrEmpty(color)).ToArray();

with out LINQ:

List<string> tempListColors = new List<string>();
foreach (string color in myColors)
{
if (!string.IsNullOrEmpty(color))
tempListColors.Add(color);
}
myColors = tempListColors.ToArray();

output:

Array of count=4 with values as "Red", "Blue","Green", "pink"


  Discuss Add Comment    
Add rating Rate this resource   
About the Contributor
Member Since : 10 Dec 2012
Member Points (Level) : 8646  (Professional)
Location : INDIA
Home Page : http://dotnetmirror.com
About : I am admin of this site.
Rate this resource
 
Add your Comment
Name Email WebSite
Captcha Refresh


Comments (0)
No comments found, click here to add comment.