【发布时间】:2013-12-18 11:58:06
【问题描述】:
我有这个检查与表达式匹配的数据行:
DataRow[] foundRows = this.callsTable.Select(searchExpression);
我如何检查它是否返回一些数据行,所以基本上如果它没有返回,就不要执行 if 函数中的操作?
我试过了:
if (foundRows != null) { }
【问题讨论】:
标签: c# linq if-statement datatable
我有这个检查与表达式匹配的数据行:
DataRow[] foundRows = this.callsTable.Select(searchExpression);
我如何检查它是否返回一些数据行,所以基本上如果它没有返回,就不要执行 if 函数中的操作?
我试过了:
if (foundRows != null) { }
【问题讨论】:
标签: c# linq if-statement datatable
您可以使用 array 的 Length 属性来检查是否有任何行
if (foundRows.Length == 0)
【讨论】:
Count() 尽可能优化。从the documentation,如果源的类型实现ICollection<T>,则该实现用于获取元素的计数。否则,此方法确定计数。
可以使用Count方法进行验证:
if (foundRows.Count() == 0)
【讨论】:
foundRows是一个数组,不能使用Count方法,需要检查数组的长度。
您可以使用 LINQ 执行以下操作
var areThereAny = foundRows.Any();
var count = foundRows.Count();
如果您只想找出是否有任何行符合您的条件,您可以执行以下操作:
var anyThatMatch = this.callsTable.Any(selectCondition);
【讨论】:
像这样检查数组的长度
if (foundRows.Length > 0)
{
//Your code here
}
或者您也可以使用 Count() 进行检查
if (foundRows.Count() > 0)
{
//Your code here
}
【讨论】: