2017-02-15 53 views
0

傳遞string [] list使用IN運算符從db中檢索數據。如何在WPF中的嵌套Select語句中使用字符串[]列表C#應用程序

Select col1, col2, col3 from tablename where col4 IN (// the list goes here);

列表中的項目數量可以超過1000個,並由於Oracle不允許在列表中通過1000個多表情,我正在尋找一種替代。現在的問題:

1:我不想在數據庫中創建臨時表,因爲我沒有足夠的權限。

2:不確定是否有多個IN可以工作,因爲這個列表是動態的,並且需求爲no。的IN運營商可能會改變

所以這裏的東西我想做的事,但不知道如何:

SELECT col1, col2, col3 from tablename where col4 IN (Select col4 from "string[]list";)

是否有嵌套查詢語句中使用此列表的方法嗎? 或者在尊重上述問題的同時使用超過1000個表達式的其他選擇? 任何幫助,將不勝感激。謝謝 !

P.S.我簡化了查詢,爲有意願的人提供幫助。原始查詢遠不止於此!

UPDATE

所以經過@Barry奧凱恩給我一個方法來解決這個問題,我想出了以下解決方案:

IEnumerable<string> List = arrExcelItems; //arrExcelItems is the actual list with more than 1000 expressions 
IList<IEnumerable<string>> listofLists = new List<IEnumerable<string>>(); 
    List<string> listtoQuery = new List<string>(); 
    string strCompList = null; 
    string extQuery = null; 


    string extQuery = null; string Query = "Select col1, col2, col3 from tablename where col4 IN "; 

       for (int i = 0; i < List.Count(); i += 20) 
       { 
        listofLists.Add(List.Skip(i).Take(20).ToList()); //Adding every 20 items of list to the listoflists[i] index 

        for (int j = 0; j < listofLists.Count; j++) //possibility of improving OR removing this for loop and use only string.Join method. 
        { 
         strCompList = string.Join("','", listofLists[j].ToArray()); 
        } 

        strCompList = "('" + strCompList + "')"; //concatenating every list in listofLists[i] for IN operator 

        arrList.Add(strCompList); 
       } 

    for (int i = 0; i < listtoQuery.Count; i++) 
      { 
        extquery = string.Join(" OR IN ", listtoQuery); //      
      } 

      Query += extQuery; 

    //resultant query after concatenation 

    Query = "Select col1, col2, col3 from tablename where col4 IN listtoQuery[0] OR col4 IN listtoquery[1] OR col4 IN...."; 

所以最後我找到了一個解決方案,我可以將我的列表分區爲多個列表並將它們各自傳遞給多個IN運營商。我希望它對其他人有用。 注:由於我對編程世界非常陌生,因此我願意接受建議和改進。

+0

如果有超過1K的搜索值,將它們分批分成1k個值並且多次運行查詢(每個批次)? – ASh

+0

@ASh問題是,它不只是一個查詢,而是查詢從數據庫中獲取信息集。這已經非常複雜了,我不想通過重新運行查詢來改善性能。但我想這是目前唯一可能的解決方案。謝謝你:) – Zak29

回答

0

因此,讓我們假設你有一個IList<int> items其中2300項。

由於1000項目是極限允許其分解和循環會這樣

DataTable dt; 
int iterator = items.Count/1000; 
int remainder = items.Count % 1000; 

for (int i = 0; i < iterator; i++) 
{ 
    if (i ==0) 
     dt = new DataTable(); 

    //Get 1000 items for the in clause 
    var inStr = string.Join(items.Take(1000).skip(i*1000).ToArray(),','); 
    // Build the sql query 
    var query = string.Format("Select col1, col2, col3 from tablename where col4 IN ({0})", inStr); 
    // Execute the query and add the items to the data table 
    dt.Merge(// DataTable from the query); 
} 

var finalIn = string.Join(items.Take(remainder).Skip(iterator*1000).ToArray(), ','); 
// Build and execute the query for the final time and add to dt again 
dt.Merge(// DataTable from the query); 

如果你有一個字符串或別的東西,然後將代碼修改必要。

+0

謝謝@Barry O'Kane我試圖用你的想法,而是實現它與** IEnumerable **。如果我找不到所需的解決方案,我必須堅持這一點。 – Zak29

+0

只要T繼承自對象,就可以爲'IN'子句中的每個項目添加參數,並將參數字符串追加到'inStr'中。讓我知道你是否希望答案更新以反映這一點。 –

+0

我用我自己的解決方案更新了我的文章。有一個外觀和感覺可以批評:) – Zak29