0

My application is asp.net MVC using Linq-to-Sql. I am trying to use the following to filter a view.

I have already added the filter to my SQL Server view using:

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

It works well when I run it in SQL Server Management Studio, however I still see the entries in my application.

I tried to filter it again in my repository using:

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

Recstatus is smallint

I get the following error:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)

I would appreciate your assistance, thanks in advance.

4

4 回答 4

2

两个问题

  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();
}
于 2013-05-04T07:02:01.663 回答
2

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

List<vw_Client_info> searchResult = 
    new List<vw_Client_info>().Where(c=> c.Recstatus != 1).ToList();
于 2013-05-04T06:54:03.947 回答
0

可枚举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();
于 2013-05-04T06:59:47.347 回答
0

这是因为您正在从 Select 返回一个匿名类型,并且您正试图将其存储在List<vw_Client_info>. 投影总是创建匿名类型。这样你就可以在尾部存储IEnumerable或使用ToList()

于 2013-05-04T07:01:52.537 回答