Cannot implicitly convert type 'System.Linq.IQueryable<Apps.Model.Applicant> to 'Apps.Model.Applicant'. An Explicit conversion Exists ASP.NET MVC3 -
public viewresult index(string currentfilter, string searchstring, int? page) { if (request.httpmethod == "get") { searchstring = currentfilter; } else { page = 1; } viewbag.currentfilter = searchstring; var connstring = configurationmanager.connectionstrings["applicantdb"].connectionstring; list<applicant> instructors = new list<applicant>(); using (var conn = new sqlconnection(connstring)) { conn.open(); var query = new sqlcommand("select top 50 applicant_id, applicant_lastname, applicant_firstname, applicant_middlename, applicant_address, applicant_city"+ " applicant", conn); var reader = query.executereader(); int currentpersonid = 0; applicant currentinstructor = null; while (reader.read()) { var personid = convert.toint32(reader["applicant_id"]); if (personid != currentpersonid) { currentpersonid = personid; if (currentinstructor != null) { instructors.add(currentinstructor); } currentinstructor = new applicant(); currentinstructor.applicant_id = convert.toint32(reader["applicant_id"].tostring()); currentinstructor.applicant_lastname = reader["applicant_lastname"].tostring(); currentinstructor.applicant_firstname = reader["applicant_firstname"].tostring(); currentinstructor.applicant_middlename = reader["applicant_middlename"].tostring(); currentinstructor.applicant_address = reader["applicant_address"].tostring(); currentinstructor.applicant_city = reader["applicant_city"].tostring(); } if (!string.isnullorempty(searchstring)) { currentinstructor = instructors.asqueryable().where(s => s.applicant_lastname.toupper().contains(searchstring.toupper()) || s.applicant_firstname.toupper().contains(searchstring.toupper())); } } if (currentinstructor != null) { instructors.add(currentinstructor); } reader.close(); conn.close(); } int pagesize = 10; int pagenumber = (page ?? 0); return view(instructors.topagedlist(pagenumber, pagesize)); }
error in line
if (!string.isnullorempty(searchstring)) { currentinstructor = instructors.asqueryable().where(s => s.applicant_lastname.toupper().contains(searchstring.toupper()) || s.applicant_firstname.toupper().contains(searchstring.toupper())); }
this first time encountered type of error . . wasted 2 hours in kind of error hope can me in situation. . advance thank willing help. . appreciated :) kudos !!
as error says, trying assign single object list.
currentinstructor = instructors.asqueryable().where(s => s.applicant_lastname.toupper().contains(searchstring.toupper()) || s.applicant_firstname.toupper().contains(searchstring.toupper()));
if there can 1 result need use singleordefault() , if there multiple records, use firstordefault() extracts first record result set.
instructors.asqueryable().where(s => s.applicant_lastname.toupper().contains(searchstring.toupper()) || s.applicant_firstname.toupper().contains(searchstring.toupper())).firstordefault();
Comments
Post a Comment