7

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

RTipton
  • 503
  • 2
  • 8
  • 11

6 Answers6

17

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.

ljs
  • 37,275
  • 36
  • 106
  • 124
  • Ouch. I edited this one too many times I think... now it's a community post! Oh well. :-) – ljs Sep 08 '08 at 10:07
5

What's wrong with

(aDataSet.Tables.Count == 0)

?

Joe Ratzer
  • 18,176
  • 3
  • 37
  • 51
  • it seems that the author defines "empty dataset" as dataset with no tables or with any number of empty tables. – vitule Aug 27 '09 at 13:48
  • If you look at the original post (click on edit link) you'll see that "or tables.count" has been added. Prior to that change, my question was sensible... – Joe Ratzer Aug 27 '09 at 15:47
  • The problem with that is that as of 2013 ASP.NET 4.0 is that the dataSet may be null in which case that check crashes – philw Jun 07 '13 at 08:46
  • Good point, but the question is about checking if the dataSet is empty rather than not null. If you think it's a better answer I'll add a null check? – Joe Ratzer Jun 07 '13 at 13:17
3

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;
        }
dance2die
  • 35,807
  • 39
  • 131
  • 194
2

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 );
Keith
  • 150,284
  • 78
  • 298
  • 434
  • That's a nice idea, though to be pedantic the t.Rows.Any() line won't compile as dataSet.Tables.Rows is a DataRowCollection which doesn't implement IEnumerable so .Any() is not available. – ljs Sep 07 '08 at 16:46
  • Oh, and sorry to be even more horribly critical but the extension method will not work when the dataset is null, rather a NullReferenceException will be raised. In addition IsEmpty() is returning the opposite of what it should - it indicates whether it's *not* empty!! – ljs Sep 07 '08 at 17:00
  • Ah, I didn't know that. That is very cool thank you for clearing that up :-) – ljs Sep 08 '08 at 08:44
0
#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

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

Portman
  • 31,785
  • 25
  • 82
  • 101