Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert this IQueryable<Patient> to DbSet<Patient> ?

This should be straightforward but I'm getting tripped up on the line inside the IF block. The error on that line is

"Cannot implicitly convert type 'System.Linq.IQueryable[Patient]' to 'System.Data.Entity.DbSet[Patient]'. An explicit conversion exists (are you missing a cast?)"

I tried appending a variety of extensions (AsQueryable(), ToList(), AsEnumerable(), etc) after .Contains() to no avail.

What am I missing here? This project is build using the MVC 4 Beta and EF4

public ActionResult SearchIndex(string searchString)
{
    var patients = this.db.Patients;

    if (!String.IsNullOrEmpty(searchString))
    {
        patients = patients.Where(p => p.LastName.Contains(searchString));
    }

    return View(patients.ToList());

}
like image 857
Joe Avatar asked May 09 '12 15:05

Joe


2 Answers

Declare patients explicitly as IQueryable<Patient>

IQueryable<Patient> patients = this.db.Patients;

Or call AsQueryable on it:

var patients = this.db.Patients.AsQueryable();
like image 86
nemesv Avatar answered Sep 20 '22 22:09

nemesv


You can just change the type of patients to make this work:

IQueryable<Patient> patients = this.db.Patients;
like image 44
Nick Butler Avatar answered Sep 23 '22 22:09

Nick Butler