C# DataRow Empty-check

后端 未结 11 1088
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-15 05:13

I got this:

 DataTable dtEntity = CreateDataTable();
 drEntity = dtEntity.NewRow();

Then I add data to the row (or not). Lots of code, real

相关标签:
11条回答
  • 2020-12-15 05:48
    public static bool AreAllCellsEmpty(DataRow row)
    {
      if (row == null) throw new ArgumentNullException("row");
    
      for (int i = row.Table.Columns.Count - 1; i >= 0; i--)
        if (!row.IsNull(i))
          return false;
    
      return true;
    }
    
    0 讨论(0)
  • 2020-12-15 05:48

    To delete null and also empty entries Try this

      foreach (var column in drEntitity.Columns.Cast<DataColumn>().ToArray())
                {
                    if (drEntitity.AsEnumerable().All(dr => dr.IsNull(column) | string.IsNullOrEmpty( dr[column].ToString())))
                        drEntitity.Columns.Remove(column);
                }
    
    0 讨论(0)
  • 2020-12-15 05:51

    I created an extension method (gosh I wish Java had these) called IsEmpty as follows:

    public static bool IsEmpty(this DataRow row)
    {
        return row == null || row.ItemArray.All(i => i is DBNull);
    }
    

    The other answers here are correct. I just felt mine lent brevity in its succinct use of Linq to Objects. BTW, this is really useful in conjunction with Excel parsing since users may tack on a row down the page (thousands of lines) with no regard to how that affects parsing the data.

    In the same class, I put any other helpers I found useful, like parsers so that if the field contains text that you know should be a number, you can parse it fluently. Minor pro tip for anyone new to the idea. (Anyone at SO, really? Nah!)

    With that in mind, here is an enhanced version:

    public static bool IsEmpty(this DataRow row)
    {
        return row == null || row.ItemArray.All(i => i.IsNullEquivalent());
    }
    
    public static bool IsNullEquivalent(this object value)
    {
        return value == null
               || value is DBNull
               || string.IsNullOrWhiteSpace(value.ToString());
    }
    

    Now you have another useful helper, IsNullEquivalent which can be used in this context and any other, too. You could extend this to include things like "n/a" or "TBD" if you know that your data has placeholders like that.

    0 讨论(0)
  • 2020-12-15 05:51

    I did it like this:

    var listOfRows = new List<DataRow>();
    foreach (var row in resultTable.Rows.Cast<DataRow>())
    {
        var isEmpty = row.ItemArray.All(x => x == null || (x!= null && string.IsNullOrWhiteSpace(x.ToString())));
        if (!isEmpty)
        {
            listOfRows.Add(row);
        }
    }
    
    0 讨论(0)
  • 2020-12-15 05:56

    A simple method along the lines of:

    bool AreAllColumnsEmpty(DataRow dr)
    {
     if (dr == null)
     {
      return true;
     }
     else
     {
      foreach(var value in dr.ItemArray)
      {
        if (value != null)
        {
          return false;
        }
      }
      return true;
     }
    }
    

    Should give you what you're after, and to make it "nice" (as there's nothing as far as I'm aware, in the Framework), you could wrap it up as an extension method, and then your resultant code would be:

    if (datarow.AreAllColumnsEmpty())
    {
    }
    else
    {
    }
    
    0 讨论(0)
  • 2020-12-15 05:57

    You could use this:

    if(drEntity.ItemArray.Where(c => IsNotEmpty(c)).ToArray().Length == 0)
    {
        // Row is empty
    }
    

    IsNotEmpty(cell) would be your own implementation, checking whether the data is null or empty, based on what type of data is in the cell. If it's a simple string, it could end up looking something like this:

    if(drEntity.ItemArray.Where(c => c != null && !c.Equals("")).ToArray().Length == 0)
    {
        // Row is empty
    }
    else
    {
        // Row is not empty
    }
    

    Still, it essentially checks each cell for emptiness, and lets you know whether all cells in the row are empty.

    0 讨论(0)
提交回复
热议问题