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

    }

此行有错误

 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 !

4

2 回答 2

3

As the error says, you are trying to assign single object to list.

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

If there can be only one result you need to use SingleOrDefault() and if there are multiple records, use FirstOrDefault() which extracts first record from result set.

instructors.AsQueryable().Where(s => s.APPLICANT_Lastname.ToUpper().Contains(searchString.ToUpper())
                                               || s.APPLICANT_FirstName.ToUpper().Contains(searchString.ToUpper())).FirstOrDefault();
于 2013-05-14T03:22:12.293 回答
2

You probably want the first applicant in the list.

currentInstructor = instructors
    .AsQueryable()
    .Where(s => s.APPLICANT_Lastname.ToUpper().Contains(searchString.ToUpper()) || s.APPLICANT_FirstName.ToUpper().Contains(searchString.ToUpper()))
    .FirstOrDefault();
于 2013-05-14T03:22:28.910 回答