c# - 为什么此动态列表比较失败?

标签 c# asp.net linq

我有一个返回动态结果的搜索。所以如果没有找到结果,我会尝试只显示一个标签。我遇到的问题是我不知道如何计算结果,因为它是动态的并且不等于一种类型。

错误信息是:

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)
{

}

这样你就可以将实际的 List0 进行比较,这显然会失败。

只是做:

if(results.Count != 0 || Cresults.Count !=0)
{

}

或者:

if(results.Any() || Cresults.Any())
{

}

关于c# - 为什么此动态列表比较失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13781571/

相关文章:

c# - 为什么 OdbcCommand.ExecuteScalar() 抛出 AccessViolationException?

c# - 如何在 PagedList.MVC 中自定义设计?

sql - 这个 linq 插入有什么问题吗?好像很多SQL为了简单的操作

c# - 根据属性值列出选择的对象范围

c# - 使用 Viewbox 缩放包含标签和文本框的网格

c# - 磁盘读/写时间的性能计数器

c# - 创建一个将文本文件作为参数的函数

c# - 定位代码在 li 内生成的数据列表项

asp.net - 在 asp.net mvc 中动态添加文本框并将值保存到数据库中

c# - 将嵌套的 for 循环转换为单个 LINQ 语句