Select 开头不是的项目

Select Items that Do Not StartsWith

我的 WinForm (Visual Studio 2017) 有问题。

在此之前,我认为向您提供一些详细信息可能会使我们所有人受益。我只会 post 我认为与问题相关的细节,所以如果您认为我遗漏了什么,请随时告诉我。也问我是否我没有正确解释某些部分。 我使用 DataTableReader.GetSchemaTable 方法来做事,如果这完全相关的话。

我希望列表的元素显示在 Textbox 中,然后将其复制到文本文件 ecc ecc 中。在 Textbox 上方,我创建了一个 DataGrid,您可以在其中看到 NameField,并且有一个名为 "Able" 的复选框,用于确定这些字段是否会显示(选中)下面的 Textbox 或不(未选中)。

首先,我制作了一个 class,在其中设置了我想要的集合属性,例如名称和条件 "Able"。我默认将其设置为真(此处未显示),因此对于所有 NameField,当前选中 DataGridView 中的勾号。这意味着它们将出现在下面的 Textbox 中,准备成为 "filetexted".

public class Info {
    public string NameField {get; set;}
    public bool Able {get; set;}
}

然后在另一个 class 中,我制作了一个 Observable 集合,其中将填充上面创建的那些 NameField(使用 SqlDataAdapter 中的函数 Fill,我不会在这里显示)。

public class Do {
    public ObservableCollection<Info> Projects = new ObservableCollection<Info>();
}

最后我对该集合中的元素进行了排序,以便首先显示以特定字母开头的元素(另一个用户帮助我解决了这个问题)。

var change = Projects.OrderByDescending(c => 
    c.NameField.StartsWith("OS")).ToList();
Projects.Clear();
foreach (Info aInfo in change) {
    Projects.Add(aInfo);
}

现在我需要的是,同一个集合中所有不以这些字母开头的元素都将禁用对 Able 的检查。因此,这意味着 DataGrid 将取消选中 "Able" 下的勾号,而那些精确的 NameField 将不会出现在 TextBox.

我在这方面遇到了真正的问题,我似乎找不到解决方案,所以我想问问你们。提前谢谢你。

这里有一个关于如何对您的集合进行排序的建议,以便以 您的 sort/search 条件列在顶部,因此元素中的所有 .Able 字段 满足 sort/search 条件的为真,其余为假。

代码清单包括用于您的 Info 对象的 class,一个 class 以及用于对您的集合进行排序和更新的方法。最后 class 带有一种方法来测试它是否一切正常。 我希望下面的代码清单有足够的注释,可以自我解释。

using System.Linq;
using System.Diagnostics;
using System.Collections.ObjectModel;


namespace TestSpace
{
    public class Info //Your original class for the project info objects.
    {
        public string NameField { get; set; }
        public bool Able { get; set; }
    }

    public class ProjectSorter  //COMMENT: Renamed your "Do" class to "ProjectSorter" since it seems more understandable.
    {
        // COMMENT:
        // In this proposed solution the SortProjects method is changed so that it now takes two parameters.        
        // The parameters are the project collection to be sorted, and the sort/search criteria.The procject  
        // collection to be sorterd is sent by ref so that any changes to it is reflected back to the caller.
        // (See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref for more
        // information on passing objects by ref.)

        public void SortProjects(ref ObservableCollection<Info> projectCollectionToSort, string sortCriteria)
        {
            // As allready solved this will sort the collection based on the sort criteria into a new list.
            var updatedProjectList =
            projectCollectionToSort.OrderByDescending(c => c.NameField.StartsWith(sortCriteria)).ToList();

            // Using the list's .ForeEach method we iterate through the new list and uses a lambda expression
            // to set .Able to true or false based on the sort/search criteria.
            updatedProjectList.ForEach(c => {
                if (c.NameField.StartsWith(sortCriteria)) { c.Able = true; } else { c.Able = false; }
            });

            // We then reset our collection to a new ObservableCollection<Info> and fills it with the new 
            // sorted and updated list.
            projectCollectionToSort = new ObservableCollection<Info>(updatedProjectList);

            // Work done.
        }
    }

    public static class TestTheCode
    {
        // Method to test the ProjectSorter.SortProjects() method.
        public static void RunSortCollectionTest()
        {
            ProjectSorter projectSorter = new ProjectSorter();

            // Just for the purpose of this example an example collection is populated
            // here with some data to work with.
            // As you describe for your list the default value for Able is set to true.
            ObservableCollection<Info> projects = new ObservableCollection<Info>()
            {
                new Info { NameField="PER", Able = true},
                new Info { NameField="ALEX", Able = true},
                new Info { NameField="OSCAR", Able = true},
                new Info { NameField="ANDY", Able = true}
            };

            // We sort the collection "projects" by sending it by ref to the projectSorter.SortProjects()
            // method, together with the sort/search criteria.
            projectSorter.SortProjects(ref projects, "OS");

            // To display that the collection "projects" are now sorted as intended, and that all the
            // .Able fields are satt correctly true or false, we iterate through the projects collection 
            // and print the values for NameField and Able to the Output window (debug).
            foreach (Info aInfo in projects)
            {
                Debug.Print(aInfo.NameField + "  -->  " + aInfo.Able);
            }
        }
    }
}

为了 运行 测试只需从您的代码中调用 TestTheCode.RunSortCollectionTest()