C# - 使用字符串拆分器查询多个逗号分隔值搜索参数

发布于 2024-10-19 12:09:51 字数 4299 浏览 1 评论 0原文

要求:我有一个用 C# 编写的 Windows 应用程序,目前只能按 1 个 Corp 值搜索 .CSV 文件(例如:按 Corp 搜索:160),并且我希望它搜索由逗号(例如:按公司搜索:160, 220, 310, 550, 610)。

问题:如何搜索以逗号分隔的多个公司值? (例如:按公司搜索:160、220、310、550、610)。 该字符串必须用逗号值分割,并且当有 x # 个逗号(例如:Corp1、Corp2、Corp3)时,每个字符串的值必须设置为 x+1 个变量(例如:Corp1、Corp2、Corp3)有 2 个逗号,因此必须定义 3 个变量),然后对每个 corp 值循环搜索 x+1 次

以下是定义 corp 值的 MainForm.cs 代码中的相关代码:

    private SearchCriteria GetSearchCriteria()
    {

        // Initialize Search Parameters object
        SearchCriteria sc = new SearchCriteria(textBoxCorp.Text,
                                               textBoxOrderNumber.Text,
                                               textBoxCampaign.Text,
                                               textBoxCity.Text,
                                               comboBoxState.Text,
                                               textBoxZip.Text,
                                               folderBrowserDialog1.SelectedPath,
                                               folderBrowserDialog2.SelectedPath,
                                               radioButtonAny.Checked,
                                               radioButtonAll.Checked);

        return sc;
    }

    private void textBoxCorp_TextChanged(object sender, EventArgs e) { }

这是 SearchProcess.cs 中的相关代码,其中引入了 corp 值并检查它是否与搜索匹配:

    // Function runs in worker thread and emulates long process.
    public void Run()
    {
        m_sc = (SearchCriteria)m_form.Invoke(m_form.m_DelegateGetSearchCriteria);
        // Display parameters
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Corp: " + m_sc.get_Corp() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on OrderNumber: " + m_sc.get_OrderNumber() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Campaign: " + m_sc.get_Campaign() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on City: " + m_sc.get_City() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on State: " + m_sc.get_State() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Zip: " + m_sc.get_Zip() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Source Path: " + m_sc.get_SourcePath() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Target Path: " + m_sc.get_TargetPath() });
        if (m_sc.get_SearchAND()==true)
        {
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search for All" });
        }
        else
        {
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search for Any" });
        }

            // Found should be true if ANY of the criteria are met. 
            // This is an OR logic gate
            // If any of the given fields do match then it is a true
            if (m_sc.get_SearchOR().Equals(true))
            {
                // Check for the Corp type match
                if (m_sc.get_Corp() != "" && String.Compare(AgentID, m_sc.get_Corp()) == 0)
                {
                    found = true;
                }
        }

            // Copy the file if ANY of the search criteria have been met
            if (found)
            {

                m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"FOUND: Order_No: " + Order_No +
                                                                        " barcode: " + barcode +
                                                                        " MailerCode: " + MailerCode +
                                                                        " AgentID: " + AgentID +
                                                                        " City: " + City +
                                                                        " State: " + State +
                                                                        " ZIP: " + ZIP});
                //passes values to TransferFile 
                TransferFile(directory, barcode, AgentID, ZIP);
            }
        } // end for that finds each matching record 

任何帮助将不胜感激!谢谢!!!

Requirement: I have a windows application written in C# that currently will only search a .CSV file by 1 Corp value (ex: Search by Corp: 160) and I want it to search multiple Corp values seperated by commas (ex: Search by Corp: 160, 220, 310, 550, 610).

Problem: How do I search multiple corp values seperated by commas??
(ex: Search by Corp: 160, 220, 310, 550, 610).
The string has to be split by the comma value and the value of each string must be set to x+1 # of variables (ex: Corp1, Corp2, Corp3) when there are x # of commas (ex: Corp1, Corp2, Corp3 has 2 commas so 3 variables would have to be defined) and then looping the search x+1 times for each of the corp values

Here is the relevant code in MainForm.cs code where the corp value is defined:

    private SearchCriteria GetSearchCriteria()
    {

        // Initialize Search Parameters object
        SearchCriteria sc = new SearchCriteria(textBoxCorp.Text,
                                               textBoxOrderNumber.Text,
                                               textBoxCampaign.Text,
                                               textBoxCity.Text,
                                               comboBoxState.Text,
                                               textBoxZip.Text,
                                               folderBrowserDialog1.SelectedPath,
                                               folderBrowserDialog2.SelectedPath,
                                               radioButtonAny.Checked,
                                               radioButtonAll.Checked);

        return sc;
    }

    private void textBoxCorp_TextChanged(object sender, EventArgs e) { }

Here is the relevant code in SearchProcess.cs where the corp value is brought in and checked to see if it matches the search:

    // Function runs in worker thread and emulates long process.
    public void Run()
    {
        m_sc = (SearchCriteria)m_form.Invoke(m_form.m_DelegateGetSearchCriteria);
        // Display parameters
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Corp: " + m_sc.get_Corp() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on OrderNumber: " + m_sc.get_OrderNumber() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Campaign: " + m_sc.get_Campaign() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on City: " + m_sc.get_City() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on State: " + m_sc.get_State() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Zip: " + m_sc.get_Zip() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Source Path: " + m_sc.get_SourcePath() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Target Path: " + m_sc.get_TargetPath() });
        if (m_sc.get_SearchAND()==true)
        {
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search for All" });
        }
        else
        {
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search for Any" });
        }

            // Found should be true if ANY of the criteria are met. 
            // This is an OR logic gate
            // If any of the given fields do match then it is a true
            if (m_sc.get_SearchOR().Equals(true))
            {
                // Check for the Corp type match
                if (m_sc.get_Corp() != "" && String.Compare(AgentID, m_sc.get_Corp()) == 0)
                {
                    found = true;
                }
        }

            // Copy the file if ANY of the search criteria have been met
            if (found)
            {

                m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"FOUND: Order_No: " + Order_No +
                                                                        " barcode: " + barcode +
                                                                        " MailerCode: " + MailerCode +
                                                                        " AgentID: " + AgentID +
                                                                        " City: " + City +
                                                                        " State: " + State +
                                                                        " ZIP: " + ZIP});
                //passes values to TransferFile 
                TransferFile(directory, barcode, AgentID, ZIP);
            }
        } // end for that finds each matching record 

Any help would be greatly appreciated! Thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一梦等七年七年为一梦 2024-10-26 12:09:51

不完全确定您在做什么,您进行搜索的数据源在哪里,所以只是猜测:

string sCorpFilter = m_sc.get_Corp();
IEnumerable<string> corps = null;

if (! string.IsNullOrEmpty(sCorpFilter))
{
  corps = sCorpFilter.Split(",".ToCharArray(),
   StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim());
}
[...]
if (corps != null && corps.Contains(AgentID))
{
  found = true;
}

Not completely sure what you are doing, where is the data source you make the search on, so just guessing:

string sCorpFilter = m_sc.get_Corp();
IEnumerable<string> corps = null;

if (! string.IsNullOrEmpty(sCorpFilter))
{
  corps = sCorpFilter.Split(",".ToCharArray(),
   StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim());
}
[...]
if (corps != null && corps.Contains(AgentID))
{
  found = true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文