检查列表是否在 C# 中为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18867180/
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
Check if list is empty in C#
提问by lakshganga
I have a list of objects populated from a database. I need to display an error message if the list is empty and display a grid view otherwise.
我有一个从数据库填充的对象列表。如果列表为空,我需要显示错误消息,否则显示网格视图。
How do I check if a List<T>
is empty in C#?
如何检查List<T>
C# 中的a是否为空?
回答by Tim Schmelter
Why not...
为什么不...
bool isEmpty = !list.Any();
if(isEmpty)
{
// error message
}
else
{
// show grid
}
The GridView
has also an EmptyDataTemplate
which is shown if the datasource is empty. This is an approach in ASP.NET:
所述GridView
具有也是EmptyDataTemplate
如果数据源是空的,其被示出。这是 ASP.NET 中的一种方法:
<emptydatarowstyle backcolor="LightBlue" forecolor="Red"/>
<emptydatatemplate>
<asp:image id="NoDataErrorImg"
imageurl="~/images/NoDataError.jpg" runat="server"/>
No Data Found!
</emptydatatemplate>
回答by Jeroen van Langen
What about using the Count() method.
使用 Count() 方法怎么样。
if(listOfObjects.Count() != 0)
{
ShowGrid();
HideError();
}
else
{
HideGrid();
ShowError();
}
回答by Grant Thomas
If the list implementation you're using is IEnumerable<T>
and Linq is an option, you can use Any
:
如果您使用的列表实现是IEnumerable<T>
并且 Linq 是一个选项,您可以使用Any
:
if (!list.Any()) {
}
Otherwise you generally have a Length
or Count
property on arrays and collection types respectively.
否则,您通常分别在数组和集合类型上有一个Length
orCount
属性。
回答by Baahubali
gridview itself has a method that checks if the datasource you are binding it to is empty, it lets you display something else.
gridview 本身有一个方法,可以检查您绑定它的数据源是否为空,它可以让您显示其他内容。
回答by David MacCrimmon
If you're using a gridview then use the empty data template: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatemplate.aspx
如果您使用的是 gridview,则使用空数据模板:http: //msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatemplate.aspx
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
runat="server">
<emptydatarowstyle backcolor="LightBlue"
forecolor="Red"/>
<emptydatatemplate>
<asp:image id="NoDataImage"
imageurl="~/images/Image.jpg"
alternatetext="No Image"
runat="server"/>
No Data Found.
</emptydatatemplate>
</asp:gridview>
回答by Kuzgun
If (list.Count==0){
//you can show your error messages here
} else {
//here comes your datagridview databind
}
You can make your datagrid visible false and make it visible on the else section.
您可以使您的数据网格可见 false 并使其在 else 部分可见。
回答by Moslem Ben Dhaou
You should use a simple IF
statement
你应该使用一个简单的IF
语句
List<String> data = GetData();
if (data.Count == 0)
throw new Exception("Data Empty!");
PopulateGrid();
ShowGrid();
回答by TalentTuner
var dataSource = lst!=null && lst.Any() ? lst : null;
// bind dataSource to gird source