當調試查詢時,會產生以下SQL語句:
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Description] AS [Description],
FROM [dbo].[ChargeMultipliers] AS [Extent1]
WHERE (UPPER(REPLACE([Extent1].[Description], N' ', @p__linq__0))) = (UPPER(REPLACE(@p__linq__1, N' ', @p__linq__2)))
正如你所看到的,比較也修剪在SQL語句和的String.Empty woud作爲參數,但這可能不是傳遞理解數據庫中的空白字符。
我寧願構建查詢前要做到這一點:現在
string descriptionTrim = description.Replace(" ", string.Empty).ToUpper();
var query = from c in context.ChargeMultipliers
where c.Description.Replace(" ", "").ToUpper() == descriptionTrim
select c;
生成的SQL查詢看起來很簡單,你可以很容易找到真正會在WHERE子句中進行比較:
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Description] AS [Description],
FROM [dbo].[ChargeMultipliers] AS [Extent1]
WHERE (UPPER(REPLACE([Extent1].[Description], N' ', N''))) = @p__linq__0
請發佈異常堆棧跟蹤。 –
很可能Linq to Entities無法翻譯有效SQL中的c.Description.Replace(「」,string.Empty).ToUpper(),只需嘗試使用Linq將Linq上的對象轉換爲實體。 – Vladimir