简体   繁体   English

C#将两个列表与对象进行比较

[英]C# Compare two lists with objects

i'm making simple program, and i'm stuck on simple question. 我正在编写简单的程序,而我陷入了一个简单的问题。 How can i compare two lists with objects , find that object and print all class elements that belongs to that object? 如何比较两个具有对象的列表,找到该对象并打印属于该对象的所有类元素?

 List<Skaitytojas> MyObjectList = new List<Skaitytojas>();
 List<Knyga> KnyguList = new List<Knyga>();

 private void button2_Click(object sender, EventArgs e)
    {
        Reader reader = new Reader();

        reader._surname = Convert.ToString(textBox1.Text);
        reader._id = Convert.ToInt32(textBox2.Text);

        MyObjectList.Add(reader);
        MessageBox.Show("you created new user");
    }

private void button4_Click(object sender, EventArgs e)
    {
        Book new_book = new Book();

       new_book.book_id = Convert.ToInt32(textBox3.Text);
        new_book.date1 = Convert.ToString(textBox4.Text);
        new_book.date2 = Convert.ToString(textBox5.Text);
        new_book.date3 = Convert.ToString(textBox6.Text);
       new_book._id2 = Convert.ToInt32(textBox7.Text);

        BookList.Add(new_book);
        MessageBox.Show("you added new book!");
    }

I want to compare reader._id with new_book.id2, its gonna be same number , and if i find it i want to print all information about it: book id,date1,date2 etc. But i don't know how to do it, i tried it this way: 我想将reader._id与new_book.id2进行比较,它的编号应该是相同的,如果我找到它,我想打印有关它的所有信息:book id,date1,date2等。但是我不知道该怎么做,我是这样尝试的:

public void print_book(int x, ListBox f)
    {
        List<Book> BookList = new List<Book>();
        foreach (var k in BookList)
        {
            if (x == k._id2)
            {
                f.Items.Add(x);
            }
        }
    }

Call function with button 带按钮的通话功能

private void button3_Click(object sender, EventArgs e)
    {

        int x = Convert.ToInt32(textBox2.Text);
        Metodai m = new Metodai();
        m.print_book(x, this.listBox1);
    }

When i try to do this function the list is still empty, whats wrong with it? 当我尝试执行此功能时,列表仍然为空,这怎么了? Maybe there is another way to do it? 也许还有另一种方法吗? Can you help me? 你能帮助我吗?

The line 线

foreach (var k in BookList)

You've just instantiated BookList in the line before so it's looping through nothing. 您之前仅在行中实例化了BookList,所以它没有任何循环。

You created a function variable 'BookList' with the same name as that of instance variable. 您创建了一个与实例变量同名的函数变量'BookList'。

There are two ways to fix it. 有两种解决方法。

// Fix #1
public void print_book(int x, ListBox f)
{
    //List<Book> BookList = new List<Book>(); // Comment this line
    foreach (var k in BookList)
    {
        if (x == k._id2)
        {
            f.Items.Add(x);
        }
    }
}

// Fix #2
public void print_book(int x, ListBox f)
{
    List<Book> BookList = new List<Book>(); // 'BookList' has never been used
    foreach (var k in this.BookList) // Add 'this'
    {
        if (x == k._id2)
        {
            f.Items.Add(x);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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