2016-08-15 69 views
0

我搜索了這個問題,但我認爲任何人都面臨它。我正在嘗試使用ExecuteQuery的IN操作。C#ExecuteQuery:如何使用IN運算符?

IEnumerable<TAssets> results = db.ExecuteQuery<TAssets> 
("SELECT * FROM TAssets " + 
" WHERE AssetId not in (select MatchedAssetId from TMatches where LabelId = {0})" + 
"  and CompanyId in ({1}) ", 
labelid, 
string.Join(",", companyIdList) 
); 
return results.ToList(); 

問題是的string.join( 「」,companyIdList)返回'61,70' 。然後它會嘗試將其轉換爲整數。爲什麼?我想做什麼?

ERROR:Conversion failed when converting the nvarchar value '61,70' to data type int. 
System.Data.SqlClient.SqlException (0x80131904): Conversion failed when converting the nvarchar value '61,70' to data type int. 
    at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) 
    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) 
    at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) 
    at System.Data.SqlClient.SqlDataReader.TryHasMoreRows(Boolean& moreRows) 
    at System.Data.SqlClient.SqlDataReader.TryReadInternal(Boolean setTimeout, Boolean& more) 
    at System.Data.SqlClient.SqlDataReader.Read() 
    at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReaderBase`1.Read() 
    at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext() 
    at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) 
    at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 

任何建議?或者你可以用ExecuteQuery顯示我的IN操作符用法嗎?

+0

我可能失去了一些東西很明顯,但什麼是你'db'對象在這種情況下,和你用什麼'ExecuteQuery'法?它不是'SqlCommand'或'DbContext'的一部分。 – smoksnes

+0

與大多數語言一樣,SQL解釋* *包含數字和逗號的* single *字符串與* multiple *參數不同。 –

+0

@smoksnes db是System.Data.Linq.DataContext。我可能錯誤使用ExecuteQuery函數。 – akdora

回答

0

聲明你正在建設導致:

"SELECT * FROM TAssets WHERE AssetId not in (select MatchedAssetId from TMatches where LabelId = 1) and CompanyId in ('61,70')" 

您需要單獨interprete每個值讓你的輸出是這樣的:

"SELECT * FROM TAssets WHERE AssetId not in (select MatchedAssetId from TMatches where LabelId = 1) and CompanyId in ('61','70')" 

此行

string.Join("','", companyIdList) 

在組合與領先和結束'應該做的伎倆。

的更好的方式是動態創建SqlParameters雖然

+0

這是一個很好的嘗試,但它沒有奏效。抱歉。錯誤:將nvarchar值'61','70'轉換爲數據類型int時轉換失敗。 – akdora

1

這裏的事情是,他們是參數化,這通常是一件好事。

我並不是說這是最好的解決方案,但你應該能夠做這樣的事情:

// Note that the first {} is escaped. 
var sql = string.Format(
        "SELECT * FROM TAssets WHERE AssetId not in (select MatchedAssetId from TMatches where LabelId = {{0}}) and CompanyId in ({0})", 
        string.Join(",", companyIdList)); 
IEnumerable<TAssets> results = db.ExecuteQuery<TAssets>(sql, labelid); 
return results.ToList(); 

其實際作用是,它增加了companyIds到SQL字符串,而不是讓的ExecuteQuery參數化它。只要注意sql注入並確保你的companyId陣列中只有int

sql -variable將是:

SELECT * FROM TAssets WHERE AssetId not in (select MatchedAssetId from TMatches where LabelId = {0}) and CompanyId in (61,70) 
+0

我知道這不是最好的解決方案,但它工作。謝謝 – akdora