我搜索了这个问题,但我认为任何人都面临它。我正在尝试使用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