在 C# 中,测试数据集是否为空的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In C#, what is the best way to test if a dataset is empty?
提问by RTipton
I know you can look at the row.count or tables.count, but are there other ways to tell if a dataset is empty?
我知道您可以查看 row.count 或 tables.count,但是还有其他方法可以判断数据集是否为空?
采纳答案by ljs
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().
编辑:经过适当考虑,我已经对代码进行了大量清理,我认为这更清晰。非常感谢 Keith 在使用 .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.
请注意,正如 Keith 在他的帖子的评论中正确纠正我的那样,即使数据集为空,此方法也将起作用。
回答by Joe Ratzer
What's wrong with
怎么了
(aDataSet.Tables.Count == 0)
(aDataSet.Tables.Count == 0)
?
?
回答by Portman
To be clear, you would first need to look at all the DataTables, and then look at the count of Rows for each DataTable.
为了清楚起见,您首先需要查看所有数据表,然后查看每个数据表的行数。
回答by dance2die
I have created a small static util class just for that purpose
我为此创建了一个小的静态 util 类
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 codedoes count.
我会在下面的代码中添加一些内容并完成它。编写可读的代码确实很重要。
if (DataAccessUtil.DataSetIsEmpty(ds)) {
return null;
}
回答by Keith
I think this is a place where you could use an extension method in C# 3 to improve legibility.
我认为这是一个可以在 C# 3 中使用扩展方法来提高易读性的地方。
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 );
回答by Milan Bara?
#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