简体   繁体   中英

How do I find a value inside class properties?

I have a long list of 100 students. I am building a Find dialog that searches for students by their name. Here are my class properties:

My code

public partial class FF : Window
{
    public FF()
    {
        InitializeComponent();

        List<User> items = new List<User>();
        items.Add(new User() { Name = "John Doe", Age = 42 });
        items.Add(new User() { Name = "Jane Doe", Age = 39 });
        items.Add(new User() { Name = "Sammy Doe", Age = 13 });
        lvStudents.ItemsSource = items;

    }

    public class User
    {
        public string Name { get; set; }

        public int Age { get; set; }
    }

My XAML

<DockPanel Grid.Row="0" Grid.Column="10" Grid.ColumnSpan="10" Grid.RowSpan="10">
    <ListView Name="lvStudents">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" Width="100" DisplayMemberBinding="{Binding Name}" />
                <GridViewColumn Header="Age" Width="30" DisplayMemberBinding="{Binding Age}" />
            </GridView>
        </ListView.View>
    </ListView>
</DockPanel>

** Things I tried**

    private void FindStudent(string name)
    {
        if (stud.Name.Any(str => str.Contains(name)))
        {
            MessageBox.Show("Student found!");
        }
        else
        {
            MessageBox.Show("Student not found!");
        }
    }

Every time I run the above code, I don't get any specific error. But I get wrong result that the student was not found. Why?

private void connectDb()
{
     SqlConnection conn = new SqlConnection("YOUR DATABASE LOCATION/ DATABASE PATH");
     conn.Open(); // initalize your database connection

}

private void searchForStudentName()
{
   string name;// = your textbox that holds value of students name;
   this.connectDb(); //your connection into database
   SqlCommand cmd = new SqlCommand("Select * from ['YOUR TABLE NAME'] where Name = @Name",conn);
   cmd.parameters.AddWithValue("@Name", name);
   dr= cmd.ExcecuteReader();


  if(dr.Read())//meaning, the system found the value of the input data/ name
  {
     ListViewItem lvi = new ListViewItem(dr["NAME"].ToString);// name will be displayed in listview control
     listView1.items.add(lvi);
  }else
  {
    //there is no data found
  }
}

Finally, I fixed the issue. Thanks to all those who contributed and answered me.

private void FindStudent(string name)
{
    User std = items.FirstOrDefault(s => s.Name.Contains(name));
    if (std != null)
    {
        this.Title = "Student found!";
    }
    else
    {
        this.Title = "Student NOT found!";
    }
}

Do

if (stud.Any(st => st.Name.Contains(name)))

instead of

if (stud.Name.Any(str => str.Contains(name)))

With "using System.Linq;" of course

PS "stud" as we can see in the question, is List. We call Any() method on the list of students, and pass a lambda-predicate which checks name of every student.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM