我搜索了這個問題,但我認爲任何人都面臨它。我正在嘗試使用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操作符用法嗎?
我可能失去了一些東西很明顯,但什麼是你'db'對象在這種情況下,和你用什麼'ExecuteQuery'法?它不是'SqlCommand'或'DbContext'的一部分。 – smoksnes
與大多數語言一樣,SQL解釋* *包含數字和逗號的* single *字符串與* multiple *參數不同。 –
@smoksnes db是System.Data.Linq.DataContext。我可能錯誤使用ExecuteQuery函數。 – akdora