C# - LINQ使用错误的WHERE子句挂载SELECT

时间:2018-03-09 12:02:24

标签: sql-server linq tsql where

帮助! 我把下面的代码放在LINQ-TO-SQL中。请注意,我在WHERE子句中只使用了一个字段,但LINQ在多个字段中将SELECT与WHERE组合在一起。我制作了一个PROFILE并捕获了组装好的SELECT。你能帮我强迫LINQ只在指定的字段中搜索吗?我正在使用数据库优先。

我的LINQ代码:

var query = from v in ctx.cliente
                            orderby v.razao_social
                            where v.fantasia.ToLower().Contains(pesquisa) 
                            select new
                            {
                                id_cliente = v.id_cliente,
                                razao = v.razao_social,
                                fantasia = v.fantasia,
                                cnpj = v.cnpj,
                                cidade = v.cidade.cidade1 + "/" + v.cidade.uf1,
                                fone = v.fone1
                            };

现在,我来自探查者的结果:

exec sp_executesql N'SELECT 
    [Project1].[id_cliente] AS [id_cliente], 
    [Project1].[razao_social] AS [razao_social], 
    [Project1].[fantasia] AS [fantasia], 
    [Project1].[cnpj] AS [cnpj], 
    [Project1].[endereco] AS [endereco], 
    [Project1].[bairro] AS [bairro], 
    [Project1].[cidade] AS [cidade], 
    [Project1].[uf] AS [uf]
    FROM ( SELECT 
        [Extent1].[id_cliente] AS [id_cliente], 
        [Extent1].[cnpj] AS [cnpj], 
        [Extent1].[razao_social] AS [razao_social], 
        [Extent1].[fantasia] AS [fantasia], 
        [Extent1].[endereco] AS [endereco], 
        [Extent1].[bairro] AS [bairro], 
        [Extent2].[cidade] AS [cidade], 
        [Extent3].[uf] AS [uf]
        FROM   [dbo].[cliente] AS [Extent1]
        INNER JOIN [dbo].[cidade] AS [Extent2] ON [Extent1].[id_cidade] = [Extent2].[id_cidade]
        INNER JOIN [dbo].[uf] AS [Extent3] ON [Extent2].[id_uf] = [Extent3].[id_uf]
        WHERE ( CAST( [Extent1].[id_cliente] AS nvarchar(max)) LIKE @p__linq__0 ESCAPE N''~'') OR ([Extent1].[razao_social] LIKE @p__linq__1 ESCAPE N''~'') OR ([Extent1].[fantasia] LIKE @p__linq__2 ESCAPE N''~'') OR ([Extent1].[cnpj] LIKE @p__linq__3 ESCAPE N''~'') OR ([Extent1].[endereco] LIKE @p__linq__4 ESCAPE N''~'') OR ([Extent1].[bairro] LIKE @p__linq__5 ESCAPE N''~'') OR ([Extent2].[cidade] LIKE @p__linq__6 ESCAPE N''~'') OR ([Extent3].[uf] LIKE @p__linq__7 ESCAPE N''~'') OR ([Extent1].[cep] LIKE @p__linq__8 ESCAPE N''~'') OR ([Extent1].[fone1] LIKE @p__linq__9 ESCAPE N''~'') OR ([Extent1].[fone2] LIKE @p__linq__10 ESCAPE N''~'') OR ([Extent1].[email_ordem] LIKE @p__linq__11 ESCAPE N''~'') OR ([Extent1].[referencia] LIKE @p__linq__12 ESCAPE N''~'')
    )  AS [Project1]
    ORDER BY [Project1].[razao_social] ASC',N'@p__linq__0 nvarchar(4000),@p__linq__1 nvarchar(4000),@p__linq__2 nvarchar(4000),@p__linq__3 nvarchar(4000),@p__linq__4 nvarchar(4000),@p__linq__5 nvarchar(4000),@p__linq__6 nvarchar(4000),@p__linq__7 nvarchar(4000),@p__linq__8 nvarchar(4000),@p__linq__9 nvarchar(4000),@p__linq__10 nvarchar(4000),@p__linq__11 nvarchar(4000),@p__linq__12 nvarchar(4000)',@p__linq__0=N'%conecta%',@p__linq__1=N'%conecta%',@p__linq__2=N'%conecta%',@p__linq__3=N'%conecta%',@p__linq__4=N'%conecta%',@p__linq__5=N'%conecta%',@p__linq__6=N'%conecta%',@p__linq__7=N'%conecta%',@p__linq__8=N'%conecta%',@p__linq__9=N'%conecta%',@p__linq__10=N'%conecta%',@p__linq__11=N'%conecta%',@p__linq__12=N'%conecta%'

0 个答案:

没有答案