无法评估儿童

时间:2016-09-22 08:51:04

标签: c# sql entity-framework linq

我正在尝试进行以下查询:

var query = from c in context.ChargeMultipliers where c.Description.Replace(" ", string.Empty).ToUpper() == description.ToUpper().Replace(" ", string.Empty)select c;

在上面我试图将sql字段“Description”修剪成一个没有空格的字符串,所有大写和比较描述字符串也被修剪并转换为upper。但是,作为查询的结果,我收到“儿童无法评估”。

关于为什么会发生这种情况的任何想法?

1 个答案:

答案 0 :(得分:0)

调试查询时,会生成以下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中被修改,作为参数传递,这可能不是理解数据库时的空字符。

我希望在构建查询之前执行此操作:

        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