ok so im trying to search list for a student by the last name and i keep getting a error as
Error 1 Cannot convert type 'string' to 'Project2.Person'
and i do not know how to fix it. any advice?

class StudentData
    {   private List<string> Page;
 
        public StudentData()
        {   Page = new List<string>();
        }

      

        public Person searchStudent(string Lname) //searches for a Person based on their last name
        {
            foreach(Person search in Page)
            {
                if (search.getLastName().ToLower() == Lname.ToLower())
                {
                    return search;
                }
            }
            r

Dani AI

Generated

The replies already point at the immediate symptom. To make the search reliable and maintainable, prefer working with typed Person instances and use a case-insensitive, null-safe comparison rather than ad-hoc string parsing each time. Below are two practical patterns you can apply depending on how often searches run and whether multiple people can share the same last name.

A simple, readable approach that returns the first match (safe null checks and trimming):

public Person FindByLastName(string lastName)
{
    if (string.IsNullOrWhiteSpace(lastName)) return null;
    lastName = lastName.Trim();
    return _students?
        .FirstOrDefault(p => string.Equals(p.LastName?.Trim(), lastName, StringComparison.OrdinalIgnoreCase));
}

See the docs for LINQ's FirstOrDefault and StringComparison for behavior details: FirstOrDefault and StringComparison.

If the app does many lookups or you need every match (same last name for multiple students), build an index once and reuse it:

private Dictionary<string, List<Person>> BuildLastNameIndex()
{
    return _students
        .Where(p => !string.IsNullOrWhiteSpace(p.LastName))
        .GroupBy(p => p.LastName.Trim(), StringComparer.OrdinalIgnoreCase)
        .ToDictionary(g => g.Key, g => g.ToList(), StringComparer.OrdinalIgnoreCase);
}

Use index.TryGetValue(key, out var matches) to fetch matches in O(1) time. Dictionary behavior reference: Dictionary<TKey,TValue>.

Practical tips: ensure Person exposes a LastName property (avoid Java-style getLastName() in C#), trim and normalize on insert so searches are consistent, decide whether the method returns a single Person, a list, or throws if ambiguous, and consider thread-safety if _students is shared. As and noted, look at how the collection is populated — convert raw strings to Person objects once and cache them rather than parsing on every search.

Recommended Answers

All 5 Replies

You're saying this

foreach (Person search in Page)

Which suggests Page should be something equivalent to an IEnumerable of Person. (List<Person>, IEnumerable<Person>, etc.).

However, in your Page declaration and instantiation, you have this

private List<string> Page;
...
Page = new List<string>();

Hence the error. You have a list of strings based on your declaration & instantiation, but your loop is trying to get a Person.

o ok so how would i get it to work with what i have?

something like this i would imagine:

foreach (string person in page)
{
   if (person.getLastName().ToLower() == Lname.ToLower())
                {
                    return search;
                }
}

I may be getting this very wrong as im a total n00b....

but how are you differentiating between first and last name in your string list?

presumably you're just adding to the list the first/last name as one string?

so each list item is like:

"Dan Turner"
"ROT C89"
or whatever?

so you may want to split each string into parts...

foreach (string person in page)
{
       string [] firstLast = person.Split(' ');
   if (firstLast[1].ToLower() == Lname.ToLower())
                {
                    return person;
                }
}

either than or use

if (person.ToLower().indexOf(Lname.ToLower())>-1)

o ok so how would i get it to work with what i have?

Page needs to be a List<Person> and not a List<string> in order for you to execute your search method. If Page is going to be a List<string> then you will have to change the method. How is Page being populated? I've seen its declaration and instantiation, but I do not see its population.

Step back and look at your total code and make sure your code matches your intent.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.