In C#, what is the best way to test if a dataset is empty?

后端 未结 6 768
無奈伤痛
無奈伤痛 2021-01-12 13:59

I know you can look at the row.count or tables.count, but are there other ways to tell if a dataset is empty?

相关标签:
6条回答
  • 2021-01-12 14:22

    To be clear, you would first need to look at all the DataTables, and then look at the count of Rows for each DataTable.

    0 讨论(0)
  • 2021-01-12 14:27

    I have created a small static util class just for that purpose

    Below code should read like an English sentence.

        public static bool DataSetIsEmpty(DataSet ds)
        {
            return !DataTableExists(ds) && !DataRowExists(ds.Tables[0].Rows);
        }
    
        public static bool DataTableExists(DataSet ds)
        {
            return ds.Tables != null && ds.Tables.Count > 0;
        }
    
        public static bool DataRowExists(DataRowCollection rows)
        {
            return rows != null && rows.Count > 0;
        }
    

    I would just put something like below code and be done with it. Writing a readable code does count.

            if (DataAccessUtil.DataSetIsEmpty(ds)) {
                return null;
            }
    
    0 讨论(0)
  • 2021-01-12 14:29

    I think this is a place where you could use an extension method in C# 3 to improve legibility.

    Using kronoz's idea...

    public static bool IsNotEmpty ( this dataset ) 
    {
        return dataSet != null && (
            from DataTable t in dataSet.Tables 
            where t.Rows.AsQueryable().Any()
            select t).AsQueryable().Any();
    }
    
    //then the check would be
    DataSet ds = /* get data */;
    
    ds.IsNotEmpty();
    

    Due to the fact that extension methods are always expanded by the compiler this will even work if the dataset being checked is null.

    At compile time this is changed:

    ds.IsNotEmpty();
    
    //becomes
    
    DataSetExtensions.IsNotEmpty( ds );
    
    0 讨论(0)
  • 2021-01-12 14:37

    What's wrong with

    (aDataSet.Tables.Count == 0)

    ?

    0 讨论(0)
  • 2021-01-12 14:37
    #region Extension methods
    
    public static class ExtensionMethods
    {
        public static bool IsEmpty(this DataSet dataSet)
        {
            return dataSet == null || dataSet.Tables.Count == 0 || !dataSet.Tables.Cast<DataTable>().Any(i => i.Rows.Count > 0);
        }
    }
    
    #endregion
    
    0 讨论(0)
  • 2021-01-12 14:42

    I would suggest something like:-

      bool nonEmptyDataSet = dataSet != null && 
        (from DataTable t in dataSet.Tables where t.Rows.Count > 0 select t).Any();
    

    Edits: I have significantly cleaned up the code after due consideration, I think this is much cleaner. Many thanks to Keith for the inspiration regarding the use of .Any().

    In line with Keith's suggestion, here is an extension method version of this approach:-

    public static class ExtensionMethods {
      public static bool IsEmpty(this DataSet dataSet) {
        return dataSet == null ||
          !(from DataTable t in dataSet.Tables where t.Rows.Count > 0 select t).Any();
        }
      }
    

    Note, as Keith rightly corrected me on in the comments of his post, this method will work even when the data set is null.

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