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 Answers 4

2

Seems you forget to use the ToList() method at the end. Try this:

List<vw_Client_info> searchResult = 
    new List<vw_Client_info>().Where(c=> c.Recstatus != 1).ToList();
3
  • Thank you so much Hossein, I don't get the error. Any idea why if I have filtered my view in Sql, the data still show! Thanks again.
    – hncl
    May 4, 2013 at 6:59
  • @user373721 not sure if I understood your comment. May 4, 2013 at 7:17
  • I am trying to figure out, if I have the filters in the sql view, and when I run it; it removes all entries that are equl to 1. However in my table in MVC, these enteries that are equal to 1 still showing. Thanks
    – hncl
    May 4, 2013 at 7:38
2

two problems

  1. new List<vw_Client_info>() is new list there is no data
  2. you have to call .ToList() at the end of the statement

You can try something like below

using (YourDatacontext context= new YourDatacontext(connStr))
{
    List<vw_Client_info> searchResult = 
          context.vw_Client_infos.Where(c=> c.Recstatus != 1).ToList();
}
1
  • Thank you all for your kind and quick support
    – hncl
    May 4, 2013 at 7:16
0

This is because you are returning an anonymous type from your Select and you are trying to store it in the List<vw_Client_info>. The projections always create anonymous types. so that u shd store in IEnumerable or use ToList() at tail.

0

Enumerable methods inlcuding Where do not return a List but rather they return a IEnumerable

so you could modify your code to

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

Or

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

which is same as above(compiler derives the type for you)

Or

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.