2013-05-14 55 views
0
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"+ 
        " FROM 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)); 

    } 

錯誤在這行不能將類型'System.Linq.IQueryable <Apps.Model.Applicant>隱式轉換爲'Apps.Model.Applicant'。顯式轉換不存在ASP.NET MVC3

if (!String.IsNullOrEmpty(searchString)) 
       { 

        currentInstructor = instructors.AsQueryable().Where(s => s.APPLICANT_Lastname.ToUpper().Contains(searchString.ToUpper()) 
              || s.APPLICANT_FirstName.ToUpper().Contains(searchString.ToUpper())); 
       } 

這是我第一次遇到這種類型的錯誤。 。 我在這種錯誤中浪費了將近2個小時 我希望有人能幫助我解決這種情況。 。提前感謝那些願意幫助的人。 。非常感謝:) KUDOS!

回答

3

正如錯誤所述,您正嘗試將單個對象分配給列表。

currentInstructor = instructors.AsQueryable().Where(s => s.APPLICANT_Lastname.ToUpper().Contains(searchString.ToUpper()) 
              || s.APPLICANT_FirstName.ToUpper().Contains(searchString.ToUpper())); 

如果只能有一個結果,你需要使用的SingleOrDefault(),如果有多個記錄,使用FirstOrDefault(),提取第一條記錄的結果集。

instructors.AsQueryable().Where(s => s.APPLICANT_Lastname.ToUpper().Contains(searchString.ToUpper()) 
               || s.APPLICANT_FirstName.ToUpper().Contains(searchString.ToUpper())).FirstOrDefault(); 
2

您可能想要列表中的第一個申請人。

currentInstructor = instructors 
    .AsQueryable() 
    .Where(s => s.APPLICANT_Lastname.ToUpper().Contains(searchString.ToUpper()) || s.APPLICANT_FirstName.ToUpper().Contains(searchString.ToUpper())) 
    .FirstOrDefault(); 
相關問題