2011-02-25 113 views
0

要求:我有一個用C#編寫的windows應用程序,目前只會搜索一個.corp文件(按公司搜索:160)我希望它搜索多個由逗號分隔的公司值(例如:按公司搜索:160,220,310,550,610)。C# - 查詢多個逗號分隔值查詢參數w /字符串分隔符

問題:如何搜索由逗號分隔的多個corp值? (例如:由Corp搜索:160,220,310,550,610)。 當有x個逗號(例如:Corp1,Corp2等)時,必須用逗號值分割字符串,並將每個字符串的值設置爲x + 1個變量(例如:Corp1,Corp2,Corp3) Corp3具有2個逗號所以3個變量將必須被定義),然後循環搜索X + 1次對每個CORP的值

以下是在MainForm.cs代碼相關的代碼,其中CORP值被定義:

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值帶來和檢查,看它是否阻鐵相匹配的相關代碼ch:

// 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 

任何幫助將不勝感激!謝謝!!!

+0

誰能回答這個問題? – 2011-04-07 20:07:34

回答

1

不能完全肯定自己在做什麼,哪裏是數據源,你做的搜索,所以只是猜測:

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; 
} 
+0

感謝您的迴應!數據源是一個csv文件,它沒有在evaluateCSVfile方法中調用的名稱中包含彙總 - if(!f.Contains(「TOTALS」))evaluateCSVfile(d,f); DataTable表= CSVReader.ReadCSVFile(qualifiedFileName,true)。 [...]部分缺少的代碼是什麼? – 2011-02-25 16:48:44

+0

如果有x個逗號(例如:Corp1,Copr2,Corp3有2個逗號,所以必須有3個變量),將每個字符串的值設置爲x + 1個變量(例如:Corp1,Corp2,Corp3)定義),然後爲每個x + 1#corp值抽取saerch x + 1次。 – 2011-02-25 17:05:20

+0

你需要什麼其他信息來完成這個代碼? – 2011-03-02 20:04:52