0

I have the following query:

    tblVAL tblval = db.tblVALs.Where(p => p.PID == pid);

Note that I expect to get a list of items that should be returned. tblVAL is a table in Entity Framework. NOTE THAT I NEED A LIST OF ITEMS TO BE RETURNED AS THERE CAN BE MORE THAN 1 ITEMS THAT IS RETURNS. AS SUCH, I DO NOT WANT TO USE First(), etc.

I get the following message

Cannot implicitly convert type 'System.Linq.IQuerable to wa.Models.tblVAL'. An implicit converion exist (are you missing a cast? )

Nate Pet
  • 44,246
  • 124
  • 269
  • 414

2 Answers2

1

Your query is returning a collection. You need to call SingleOrDefault, Single, First, or FirstOrDefault to get a single record.

tblVAL tblval = db.tblVALs.Where(p => p.PID == pid).SingleOrDefault();

If you want a collection, change your variable type to a collection:

IEnumerable<tblVAL> tblval = db.tblVALs.Where(p => p.PID == pid);

You can also call ToList to force query execution:

List<tblVAL> tblval = db.tblVALs.Where(p => p.PID == pid).ToList();
jrummell
  • 42,637
  • 17
  • 112
  • 171
0

If you can get many results back, the simple thing to do is to call ToList.

Become familiar with the methods of System.Linq.Enumerable and you will be an expert at linq.

Amy B
  • 108,202
  • 21
  • 135
  • 185