在Linq to SQL中使用归类

Ash*_*ani 3 c# linq sql-server linq-to-sql

想象一下这个SQL查询

select * from products order by name collate Persian_100_CI_AI asc
Run Code Online (Sandbox Code Playgroud)

现在使用Linq:

product = DB.Products.OrderBy(p => p.name); // what should I do here?
Run Code Online (Sandbox Code Playgroud)

如何申请校对?

Mie*_*oli 6

现在,EF Core 5.0 可以使用collat​​e函数实现这一点。

在您的示例中,代码将是:

product = DB.Products.OrderBy(p => EF.Functions.Collate(p.name, "Persian_100_CI_AI"));
Run Code Online (Sandbox Code Playgroud)


Gio*_*uri 5

没有直接的方法。解决方法:

在Sql Server中创建函数

CREATE FUNCTION [dbo].[fnsConvert]
    (
      @p NVARCHAR(2000) ,
      @c NVARCHAR(2000)
    )
RETURNS NVARCHAR(2000)
AS
    BEGIN
        IF ( @c = 'Persian_100_CI_AI' )
            SET @p = @p COLLATE Persian_100_CI_AI
        IF ( @c = 'Persian_100_CS_AI' )
            SET @p = @p COLLATE Persian_100_CS_AI

        RETURN @p    
    END
Run Code Online (Sandbox Code Playgroud)

将其导入模型并使用:

from o in DB.Products
orderby DB.fnsConvert(s.Description, "Persian_100_CI_AI")
select o;
Run Code Online (Sandbox Code Playgroud)