I have list of items, like this:
user = "",
name = null,
surname = "",
age = null,
nickname = ""
How to check if ALL items in the list are empty strings or null do something...
I have list of items, like this:
user = "",
name = null,
surname = "",
age = null,
nickname = ""
How to check if ALL items in the list are empty strings or null do something...
var str = "bla bla";
bool result = String.IsNullOrEmpty(str); //result is false
simply use String.IsNullOrEmpty()
to validate
this should be with every string.
having you said
If All
That means &&
if (String.IsNullOrEmpty(user) && String.IsNullOrEmpty(name) &&
String.IsNullOrEmpty(surname) && String.IsNullOrEmpty(age ) &&
String.IsNullOrEmpty(nickname ) )
{
// do something
}
The other option is String.IsNullOrWhiteSpace()
which is explaining itself and includes \t
(tabs) too.
UPDATE 1
According to your last comment, If this is an object
you can develop method
like this:
bool IsAnyNullOrEmptyInObject(object input)
{
foreach(PropertyInfo pInfo in input.GetType().GetProperties())
{
if(pInfo.PropertyType == typeof(string))
{
string value = (string)pInfo.GetValue(input);
if(string.IsNullOrEmpty(value))
{
return true;
}
}
}
return false;
}
UPDATE 2
Base on your last comment on my answer and your comment on your Question:
While it is a list of objects
Suppose your object
is:
public class MyObject
{
public string User { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string NickName { get; set; }
}
and the validation method
is
private bool IsAnyNullOrEmptyInObject(MyObject input)
{
foreach (PropertyInfo pInfo in input.GetType().GetProperties())
{
if (pInfo.PropertyType == typeof(string))
{
string value = (string)pInfo.GetValue(input);
if (string.IsNullOrEmpty(value))
{
return true;
}
}
}
return false;
}
Then you call it like this (I tried to make the code is simple so you get the full idea)
var myList = new List<MyObject>()
{
new MyObject(){User = "yy", Name = "", LastName = "", Age = 0, NickName = ""},
new MyObject(){User = "twhite", Name = "Tom", LastName = "White", Age = 0, NickName = "tom"}
};
int myListLength = myList.Count;
int emptyObjectCount = 0;
foreach (MyObject obj in myList)
{
if (!IsAnyNullOrEmptyInObject(obj)) //<<-- I used ! to negate
{
emptyObjectCount++;
};
}
if (myListLength == emptyObjectCount)
{
//Do Somthing
}
Note that in my code I use ! to negate, you may not use it as you need/not need (method name explaining itself)
You can use All
or Any
operator from linq.
using System.Linq;
var areAllItemsNull = myItems.All(x => string.IsNullOrEmpty(x));
// `Any` could give you a better performance because it will exit after
// find the first item that is not null or whitespace. All will walk through
// every item in the collection.
// `All` makes the code more readable than `Any` in my opinion.
var areAllItemsNull = !myItems.Any(x => !string.IsNullOrEmpty(x));
Try this out
List<string> listOfString = new List<string> {"", "bla", null, "Yha"};
var allNullOrWhiteSpace = listOfString.Where(s => string.IsNullOrWhiteSpace(s)).ToList();
var allValidStrings = listOfString.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
foreach (var item in allNullOrWhiteSpace)
{
//Do something with invalid strings
}
foreach (var item in allValidStrings )
{
//Do something with valid strings
}
Assuming you have List<List<string>>
because thats what it sounds like. You could use this:
bool value = false;
for (int i = 0; i < list.Count(); i++)
{
value = list[i].All(x => String.IsNullOrWhitespace(x));
}
if (value)
{
//Do something
}
Given the question and the information provided I think this is what you need. This will take a List<List<string>>
and iterate through every List<string>
and check if All
strings in that List<string>
are null or empty using String.IsNullOrEmpty()
.
If it is not List<List<string>>
but instead List<List<Object>>
and Object
has all string
properties like user
and name
. Then this simple solution will work:
List<Object> newlist = new List<Object> { };
for (int i = 0; i < list.Count(); i++)
{
newlist = list[i].Where(x => !String.IsNullOrEmpty(x.user) ||
!String.IsNullOrEmpty(x.name)).ToList();
}
if (newlist.Count() == 0)
//Do something