c#使用where语句列出

本文关键字:语句 where 使用 | 更新日期: 2023-09-27 18:12:01

我的应用程序是使用Linq到Sql的asp.net MVC。我正在尝试使用以下内容来筛选视图。

我已经使用将过滤器添加到我的SQL Server视图中

WHERE (dbo.Client.Recstatus IS NULL) OR (dbo.Client.Recstatus = 0)

当我在SQLServerManagementStudio中运行它时,它运行得很好,但我仍然可以在应用程序中看到条目。

我试图在我的存储库中再次使用进行过滤

List<vw_Client_info> searchResult = new List<vw_Client_info>().Where(c=> c.Recstatus != 1);

Recstatussmallint

我得到以下错误:

无法隐式转换类型"System"。集合。通用的IEnumerable"到"系统。集合。通用的列表'。存在显式转换(是否缺少强制转换?(

我将感谢你的帮助,提前表示感谢。

c#使用where语句列出

最后似乎忘记使用ToList()方法。试试这个:

List<vw_Client_info> searchResult = 
    new List<vw_Client_info>().Where(c=> c.Recstatus != 1).ToList();

的两个问题

  1. new List<vw_Client_info>()是新列表,没有数据
  2. 您必须在语句末尾调用.ToList()

你可以试试下面的

using (YourDatacontext context= new YourDatacontext(connStr))
{
    List<vw_Client_info> searchResult = 
          context.vw_Client_infos.Where(c=> c.Recstatus != 1).ToList();
}

这是因为您正在从Select中返回一个匿名类型,并试图将其存储在List<vw_Client_info>中。投影总是创建匿名类型。使得u shd存储在CCD_ 7中或在尾部使用CCD_。

包含Where的可枚举方法不返回List,而是返回IEnumerable

这样你就可以将你的代码修改为

IEnumerable<vw_Client_info> searchResult = 
          new List<vw_Client_info>().Where(c=> c.Recstatus != 1);

var searchResult = 
         new List<vw_Client_info>().Where(c=> c.Recstatus != 1);

与上面相同(编译器为您派生类型(

List<vw_Client_info> searchResult = 
         new List<vw_Client_info>().Where(c=> c.Recstatus != 1).ToList();