傳遞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運營商。我希望它對其他人有用。 注:由於我對編程世界非常陌生,因此我願意接受建議和改進。
如果有超過1K的搜索值,將它們分批分成1k個值並且多次運行查詢(每個批次)? – ASh
@ASh問題是,它不只是一個查詢,而是查詢從數據庫中獲取信息集。這已經非常複雜了,我不想通過重新運行查詢來改善性能。但我想這是目前唯一可能的解決方案。謝謝你:) – Zak29