Sarthak Sanghavi Sarthak Sanghavi - 1 year ago 120
C# Question

Cannot implicitly convert type <DocAdminPannel.Models.DocMaster>'.An explicit conversion exists (are you missing a cast?)

Cannot implicitly convert type
to 'System.Linq.IQueryable'. An
explicit conversion exists (are you missing a cast?)

public class DoctorController : ApiController

Doctor_infoEntities db = new Doctor_infoEntities();

public IQueryable<DocMaster> GetDocMasters(string searchString)
//var Doctor = db.DocMaster.ToList();
if (searchString == "" || searchString == null)
return db.DocMaster;
DocMaster Doctor = db.DocMaster.Find(searchString);
return Ok(Doctor);

Answer Source

Your method's signature is IQueryable<DocMaster>.

Your first return statement returns IQueryale<DocMaster> as needed but in the else you are returning the result of the Find which is a single object and wrapping it with OK function which is returning a System.Web.Http.Results.OkNegotiatedContentResult object.

Replace it with something like:

return db.DocMaster.Where(item => item.SomeProperty.Contains(searchString));

Just a bit of refactoring you can change this line:

if (searchString == "" || searchString == null)

With this:

if(string.IsNullOrEmpty(searchString)) //Or also string.IsNullOrWhiteSpace
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download