我只想知道检查 IQueryable
结果是否没有值的最佳方法是什么。
例如。如果我们有这样的方法
public static IQueryable<Table> DisplayAll()
{
var db = new DataContext();
var list= from data in db.Table select data;
return list;
}
然后我们做这样的事情
var list = DisplayAll();
if(list != null)
{
//do something --- in here even if the result set has no values it will
// go to this line. It just say `enumeration yielded no results`
}
有什么可能的方法来检查结果集是否有内容??
谢谢
最佳答案
list
在 LINQ 中永远不会是 null
;如果需要,它只会代表一个“空集合”。测试方法是使用 Any
扩展方法:
if (list.Any()) {
// list has at least one item
}
关于c# - 检查 IQueryable 结果集的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7822070/