-3

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...

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
n3x
  • 75
  • 1
  • 7
  • 2
    You have to post what you attempted and what did or didn't work. And "list of items"? Is that `List`? `List`? – jaabh Dec 16 '20 at 23:50
  • Its an List – n3x Dec 17 '20 at 00:02
  • 1
    Post the `List` that you have along with the corresponding class being used. – jaabh Dec 17 '20 at 00:11
  • You cant have a list of items like that, because there is no list here, nor a class, its a bunch of variable assignments (at best), which makes this question ambiguous (look at the answers). Show the declaration of the list you are using and the class that holds these *(maybe)* properties. Also please consider reading [ask] thoroughly. In short this question is low quality, and lacks sufficient clarity to remain open – TheGeneral Dec 17 '20 at 00:23
  • @n3x what do you mean by `Object` here? Is it an instance of some class, like `User` with properties Name, Surname, NickName, and Age as a string instead of number – Sergey Berezovskiy Dec 17 '20 at 00:26

4 Answers4

2
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)

Useme Alehosaini
  • 2,998
  • 6
  • 18
  • 26
  • Yes that works for checking each element, this is just an example.. but I have 10 different lists with something like this inside and much more items, so I need some generic method to check if my lists have all items null or empty. I found this article usefull https://stackoverflow.com/questions/31759123/how-to-check-all-properties-of-an-class-whether-null-or-empty but I need something for AllNullorEmpty – n3x Dec 16 '20 at 23:59
  • So you have list of objects maybe, in this case see updated answer. – Useme Alehosaini Dec 17 '20 at 00:00
  • @n3x answer updated – Useme Alehosaini Dec 17 '20 at 00:03
  • This works for ANY null or Empty object in list if(list.Count == 1 && ObjectsHelper.IsAnyNullOrEmptyInObject(list[0])) { //do something } but doesn't work for ALL, I need to enter into loop only IF all objects in list are null or empty. – n3x Dec 17 '20 at 00:07
  • @n3x No, you should iterate the list and pass every object in the list to this method – Useme Alehosaini Dec 17 '20 at 00:09
  • Wait for updated answer – Useme Alehosaini Dec 17 '20 at 00:09
  • @n3x answer updated – Useme Alehosaini Dec 17 '20 at 00:32
1

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));
haku
  • 4,105
  • 7
  • 38
  • 63
0
        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
        }
0

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
    
jaabh
  • 815
  • 6
  • 22