我想在我的列表中添加一个对象,但嵌套循环的组织方式使这成为不可能(C#)

本文关键字:方式使 不可能 嵌套循环 列表 我的 添加 一个对象 | 更新日期: 2023-09-27 18:28:49

所以我正在制作一个程序,它有几个类别,其中存储了多个注释。我是一名学生,我们得到了使用SQL server的localdb来保存我们的类别和注释的任务。程序使用树状视图控件显示类别和注释。

现在我正试图为我的笔记制作一个搜索栏。它必须过滤笔记的标题。

我的问题是,如何从一个类别中获取所有笔记,并将它们添加到同一类别中?现在,所有笔记都显示在不同的类别中,但名称相同。这是因为我的主循环处理注释,而内部循环处理类别。

当我把"categoriesUI1.Categories.Add(category);"放在while循环后面时,我只看到一个类别有一个音符,因为音符的while循环是先处理的。

下面是一些额外的屏幕截图,让你更好地了解我的程序是如何工作的:

过滤前:https://gyazo.com/cea5c4d9ff18fb93c9f47cf336942bc9

在"test2"上筛选后:https://gyazo.com/282240e5f1a754389688c68257893d92

connection = new SqlConnection(@"Data Source=(localdb)'mssqllocaldb;Initial Catalog=dbNotes;Integrated Security=True;MultipleActiveResultSets=true");
        categoriesUI1.tvwCategories.SelectedNode = categoriesUI1.tvwCategories.TopNode;
        int count = categoriesUI1.Categories.Count;
        if (count > 0)
        {
            for (int i = count; i > 0; i = count)
            {
                categoriesUI1.Categories.RemoveAt(0);
                count = categoriesUI1.Categories.Count;
            }
        }
        connection.Open();
        SqlCommand commandNote = new SqlCommand("SELECT ID, Cat_ID, Titel, Text, Created FROM tblNote WHERE Titel LIKE '%" + searchBarUI1.getTextValue() + "%'", connection);
        SqlDataReader readerNote = commandNote.ExecuteReader();
        if (readerNote.HasRows)
        {
            Category category = new Category();
            while (readerNote.Read())
            {
                int id = readerNote.GetInt32(0);
                int Cat_id = readerNote.GetInt32(1);
                string title = readerNote.GetString(2);
                string text = readerNote.GetString(3);
                DateTime created = readerNote.GetDateTime(4);
                string Cat_title = "";
                SqlCommand commandCategory = new SqlCommand("SELECT Titel FROM tblCategory WHERE ID = " + Cat_id, connection);
                SqlDataReader readerCategory = commandCategory.ExecuteReader();
                if (readerCategory.HasRows)
                {
                    while (readerCategory.Read())
                    {
                        Cat_title = readerCategory.GetString(0);
                        category = new Category(Cat_id, Cat_title);
                    }
                }
                Note note;
                note = new Note(id, title, text, created);
                category.Notes.Add(note);
                readerCategory.Close();
                categoriesUI1.Categories.Add(category);
            }
        }
        readerNote.Close();
        connection.Close();

我想在我的列表中添加一个对象,但嵌套循环的组织方式使这成为不可能(C#)

我给了你我的idae

1.您可以通过Searchkey获得所有Cates。2.get Add all Notes filed by searchKey and Cat_id,Add they to proper Cate

细节如下,只是一些想法,尚未测试。希望它能有所帮助。

    /// <summary>
    /// initail your  UI
    /// </summary>
    void ini()
    {

        connection = new SqlConnection(@"Data Source=(localdb)'mssqllocaldb;Initial Catalog=dbNotes;Integrated Security=True;MultipleActiveResultSets=true");
        categoriesUI1.tvwCategories.SelectedNode = categoriesUI1.tvwCategories.TopNode;
        int count = categoriesUI1.Categories.Count;
        if (count > 0)
        {
            for (int i = count; i > 0; i = count)
            {
                categoriesUI1.Categories.RemoveAt(0);
                count = categoriesUI1.Categories.Count;
            }
        }
    }



    /// <summary>
    /// this is for add all searchressult Cates to to Treeview
    /// </summary>
    /// <param name="sKey"></param>
    void SearchCate(string sKey)
    {
           connection.Open();
        SqlCommand commandCategory = new SqlCommand("SELECT Titel FROM tblCategory WHERE ID in ( 
SELECT ID  FROM tblNote WHERE Titel LIKE '%" + sKey +
                                "%') " + Cat_id, connection);

        SqlDataReader readerCategory = commandCategory.ExecuteReader();
        if (readerCategory.HasRows)
        {
            while (readerCategory.Read())
            {
                Cat_title = readerCategory.GetString(0);
                category = new Category(Cat_id, Cat_title); // new category
                categoriesUI1.Categories.Add(category);
                // then add your note's search result here to eavcgorhe
                AddNoteToACateBySearchKey(sKey,category);
            }
        }

    }

    /// <summary>
    /// add all searchresult note to cate 
    /// </summary>
    /// <param name="sKey"></param>
    /// <param name="Parent"></param>
    void AddNoteToACateBySearchKey(string sKey, Category Parent )
    {

        connection.Open();
        SqlCommand commandNote = new SqlCommand("SELECT ID, Cat_ID, Titel, Text, Created FROM tblNote WHERE Titel LIKE '%" + sKey + "%' and Cat_ID='" + Parent.Cat_id + "' ", connection);
        SqlDataReader readerNote = commandNote.ExecuteReader();
        if (readerNote.HasRows)
        {
            Category category = new Category();
            while (readerNote.Read())
            {
                int id = readerNote.GetInt32(0);
                int Cat_id = readerNote.GetInt32(1);
                string title = readerNote.GetString(2);
                string text = readerNote.GetString(3);
                DateTime created = readerNote.GetDateTime(4);
                string Cat_title = "";

                Note note;
                note = new Note(id, title, text, created); // new note
                category.Notes.Add(note);
                readerCategory.Close();


            }

        }
        readerNote.Close();
        connection.Close();
    }

只有当Cat_ID与当前类别的ID对应时,我才添加一个注释来修复它。现在,主循环处理类别,内部循环处理注释。

connection = new SqlConnection(@"Data Source=(localdb)'mssqllocaldb;Initial Catalog=dbNotes;Integrated Security=True;MultipleActiveResultSets=true");
        categoriesUI1.tvwCategories.SelectedNode = categoriesUI1.tvwCategories.TopNode;
        int count = categoriesUI1.Categories.Count;
        if (count > 0)
        {
            for (int i = count; i > 0; i = count)
            {
                categoriesUI1.Categories.RemoveAt(0);
                count = categoriesUI1.Categories.Count;
            }
        }
        connection.Open();
        SqlCommand commandCategory = new SqlCommand("SELECT ID, Titel from tblCategory", connection);
        SqlDataReader readerCategory = commandCategory.ExecuteReader();
        if (readerCategory.HasRows)
        {
            while (readerCategory.Read())
            {
                int id = readerCategory.GetInt32(0);
                string titel = readerCategory.GetString(1);
                Category category = new Category(id, titel);
                SqlCommand commandNote = new SqlCommand("SELECT ID, Cat_ID, Titel, Text, Created FROM tblNote WHERE Titel LIKE '%" + searchBarUI1.getTextValue() + "%'", connection);
                SqlDataReader readerNote = commandNote.ExecuteReader();
                if (readerNote.HasRows)
                {
                    while (readerNote.Read())
                    {
                        int catID = readerNote.GetInt32(1);
                        if (catID == id)
                        {
                            int noteID = readerNote.GetInt32(0);
                            string noteTitel = readerNote.GetString(2);
                            string noteText = readerNote.GetString(3);
                            DateTime noteCreated = readerNote.GetDateTime(4);
                            Note note = new Note(noteID, noteTitel, noteText, noteCreated);
                            category.Notes.Add(note);
                        }
                    }
                }
                if(category.Notes.Count > 0)
                    categoriesUI1.Categories.Add(category);
            }
        }
相关文章: