Ignoring accents in SQL Server using LINQ to SQL

前端 未结 4 1876
梦谈多话
梦谈多话 2020-12-17 19:20

How can I ignore accents (like ´, `, ~) in queries made to a SQL Server database using LINQ to SQL?

UPDATE:

Still haven\'t figured out how t

4条回答
  •  误落风尘
    2020-12-17 20:00

    It seems that there is a way to ignore the collation differences in Linq to SQL by using t-sql functions:

    CREATE FUNCTION [dbo].[func_ConcatWithoutCollation]
    (
        @param1 varchar(2000),
        @param2 varchar(2000)
    )
    RETURNS varchar(4000)
    AS
    BEGIN
        IF (@param1 IS NULL) SET @param1 = ''
        IF (@param2 IS NULL) SET @param2 = ''
        RETURN @param1 COLLATE Latin1_General_CS_AS + @param2 COLLATE Latin1_General_CS_AS
    END
    

    to get this function in linq to sql, there is a switch for SqlMetal: /functions. Example:

    "%ProgramFiles%\Microsoft SDKs\Windows\v7.0A\Bin\SqlMetal.exe" /server:. /database:NameOfDatabase /pluralize /code:ContextGenerated.cs /sprocs /views /functions
    

    Use this function in Linq to sql like this:

    from s in context.Services
    where context.Func_ConcatWithoutCollation(s.Description, s.Email) == "whatever"
    select s
    

    It helped me, maybe somebody finds this useful too.

提交回复
热议问题