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

Question :

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

Answer :

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

,

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().

Read More  TCL development: debug environment

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.

That’s the answer In C#, what is the best way to test if a dataset is empty?, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Read More  Running a scheduled task in a WordPress plug-in

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you