Checking if all values are null inside an initialised object

Pranav Singh

I have some code that reads data from Excel sheets & put to List.

Now here comes a scenario that user copy some valid data then leaves a lot of rows empty & again then copy valid data. This reads a lot of empty rows. I am using some old code for excel read & that might be used somewhere else so I don't wish to mess up with that.

The problem is I have a lot of object inside List that are initialised but all values are null. So I ended up checking all attribuets in if like:

if (lstConsolidatedData != null)
{
    foreach (var lst in lstConsolidatedData)
    {
         if(lst.a!=null && lst.b!=null && lst.c!=null //& so on....)
         {
           //some stuff...
         }
    }
}

I know this is not wither best or maintainable way since excel sheets can be modified & thus whole columns list code need to be changed again & again for a single column added or removed from excel.

Is there any way to check all values inside lst for null in a single statement? some thing like lst.all !=null

Any idea if not code will also work for me.

Please note lst==null is false

EDIT: using answers from below I am getting this error

enter image description here

EDIT: o.GetType().GetProperties().Any(c => c.GetValue(o) == null) works for any null but What I actually want to delete rows which contains no data in all columns not for particular one or two column. Issue is not resolved by o.Item1 != null && ... also since name/number of columns changes frequently.

Pranav Singh

I ended up digging down into old code & before turning dataset into List I deleted empty rows using code:

        //Deleting empty records from Datset
        foreach (DataTable source in result.Tables)
        {
            for (int i = 0; i < source.Rows.Count; i++)
            {
                DataRow currentRow = source.Rows[i];
                bool isEmpty = false;
                foreach (var colValue in currentRow.ItemArray)
                {
                    if (!string.IsNullOrEmpty(colValue.ToString()))
                    {
                        isEmpty = false;
                        break;
                    }
                    else
                        isEmpty = true;

                }
                if (isEmpty)
                    source.Rows[i].Delete();
            }
        }
        result.AcceptChanges();

Don't forget .AcceptChanges() since it actually save the changes done to dataset without which no records are removed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related