C# DataRow Empty-check

后端 未结 11 1129
佛祖请我去吃肉
佛祖请我去吃肉 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: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.

提交回复
热议问题