What is the best way to convert this IQueryablePatient> to a DbSetPatient>?

.net-4.0 asp.net-mvc entity-framework

Question

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());

}
1
6
5/9/2012 3:32:03 PM

Accepted Answer

Declare patients explicitly as IQueryable<Patient>

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

Or call AsQueryable on it:

var patients = this.db.Patients.AsQueryable();
12
5/9/2012 3:21:48 PM

Popular Answer

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

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


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow