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

[{'answer_id': 47839, 'author': 'Joe Ratzer', 'author_id': 4092, 'author_profile': 'https://Stackoverflow.com/users/4092', 'pm_score': 3, 'selected': False, 'text': "<p>What's wrong with</p>\n\n<p>(aDataSet.Tables.Count == 0)</p>\n\n<p>?</p>\n"}, {'answer_id': 47840, 'author': 'Portman', 'author_id': 1690, 'author_profile': 'https://Stackoverflow.com/users/1690', 'pm_score': 0, 'selected': False, 'text': '<p>To be clear, you would first need to look at all the DataTables, and then look at the count of Rows for each DataTable.</p>\n'}, {'answer_id': 47841, 'author': 'ljs', 'author_id': 3394, 'author_profile': 'https://Stackoverflow.com/users/3394', 'pm_score': 5, 'selected': True, 'text': "<p>I would suggest something like:-</p>\n\n<pre><code>  bool nonEmptyDataSet = dataSet != null &amp;&amp; \n    (from DataTable t in dataSet.Tables where t.Rows.Count &gt; 0 select t).Any();\n</code></pre>\n\n<p><strong>Edits:</strong> 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().</p>\n\n<p>In line with Keith's suggestion, here is an extension method version of this approach:-</p>\n\n<pre><code>public static class ExtensionMethods {\n  public static bool IsEmpty(this DataSet dataSet) {\n    return dataSet == null ||\n      !(from DataTable t in dataSet.Tables where t.Rows.Count &gt; 0 select t).Any();\n    }\n  }\n</code></pre>\n\n<p>Note, as Keith rightly corrected me on in the comments of his post, this method will work even when the data set is null.</p>\n"}, {'answer_id': 47979, 'author': 'dance2die', 'author_id': 4035, 'author_profile': 'https://Stackoverflow.com/users/4035', 'pm_score': 2, 'selected': False, 'text': '<p>I have created a small static util class just for that purpose</p>\n\n<p>Below code should read like an English sentence.</p>\n\n<pre><code>    public static bool DataSetIsEmpty(DataSet ds)\n    {\n        return !DataTableExists(ds) &amp;&amp; !DataRowExists(ds.Tables[0].Rows);\n    }\n\n    public static bool DataTableExists(DataSet ds)\n    {\n        return ds.Tables != null &amp;&amp; ds.Tables.Count &gt; 0;\n    }\n\n    public static bool DataRowExists(DataRowCollection rows)\n    {\n        return rows != null &amp;&amp; rows.Count &gt; 0;\n    }\n</code></pre>\n\n<p>I would just put something like below code and be done with it.\nWriting a <strong>readable code</strong> does count.</p>\n\n<pre><code>        if (DataAccessUtil.DataSetIsEmpty(ds)) {\n            return null;\n        }\n</code></pre>\n'}, {'answer_id': 48547, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 2, 'selected': False, 'text': "<p>I think this is a place where you could use an extension method in C# 3 to improve legibility.</p>\n\n<p>Using kronoz's idea...</p>\n\n<pre><code>public static bool IsNotEmpty ( this dataset ) \n{\n    return dataSet != null &amp;&amp; (\n        from DataTable t in dataSet.Tables \n        where t.Rows.AsQueryable().Any()\n        select t).AsQueryable().Any();\n}\n\n//then the check would be\nDataSet ds = /* get data */;\n\nds.IsNotEmpty();\n</code></pre>\n\n<p>Due to the fact that extension methods are always expanded by the compiler this will even work if the dataset being checked is null.</p>\n\n<p>At compile time this is changed:</p>\n\n<pre><code>ds.IsNotEmpty();\n\n//becomes\n\nDataSetExtensions.IsNotEmpty( ds );\n</code></pre>\n"}, {'answer_id': 11556606, 'author': 'Milan Barać', 'author_id': 1537223, 'author_profile': 'https://Stackoverflow.com/users/1537223', 'pm_score': 0, 'selected': False, 'text': '<pre><code>#region Extension methods\n\npublic static class ExtensionMethods\n{\n    public static bool IsEmpty(this DataSet dataSet)\n    {\n        return dataSet == null || dataSet.Tables.Count == 0 || !dataSet.Tables.Cast&lt;DataTable&gt;().Any(i =&gt; i.Rows.Count &gt; 0);\n    }\n}\n\n#endregion\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/47833', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/445815/']