我有一个返回动态结果的搜索。所以如果没有找到结果,我会尝试只显示一个标签。我遇到的问题是我不知道如何计算结果,因为它是动态的并且不等于一种类型。
错误信息是:
Operator '!=' Cannot be applied ot operands of type System.Collections.Generic.List and int
if (Page.IsValid)
{
string keyword = txtSearch.Text.Trim();
List<dynamic> results = SearchItems(keyword);
List<dynamic> Cresults = SearchContacts(keyword);
if(results != 0 || Cresults !=0)
{
//bind and return
LVI.DataSource = results;
LVI.DataBind();
// System.Threading.Thread.Sleep(500);
//Contact Bind return
LVC.DataSource = Cresults;
LVC.DataBind();
// System.Threading.Thread.Sleep(250);
lvAdmin.DataSource = results;
lvAdmin.DataBind();
LVCAdmin.DataSource = Cresults;
LVCAdmin.DataBind();
}
else{
NoResults.Visible = true;
}
最佳答案
你不能只做:
if(results != 0 || Cresults !=0)
{
}
这样你就可以将实际的 List
与 0
进行比较,这显然会失败。
只是做:
if(results.Count != 0 || Cresults.Count !=0)
{
}
或者:
if(results.Any() || Cresults.Any())
{
}
关于c# - 为什么此动态列表比较失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13781571/