Skip to content

新增SqlServer下对string.Comparestring.CompareTo方法的转译支持#2087

Merged
luoyunchong merged 1 commit intodotnetcore:masterfrom
LeaFrock:string_compare
Apr 10, 2026
Merged

新增SqlServer下对string.Comparestring.CompareTo方法的转译支持#2087
luoyunchong merged 1 commit intodotnetcore:masterfrom
LeaFrock:string_compare

Conversation

@LeaFrock
Copy link
Copy Markdown
Contributor

@LeaFrock LeaFrock commented Aug 4, 2025

以下2种方式:

var sql1 = freeSql.Select<UserInfo>()
    .Where(a => a.Name.CompareTo("LeaFrock") > 0)
    .ToSql();

var sql2 = freeSql.Select<UserInfo>()
    .Where(a => string.Compare(a.Name, "LeaFrock") > 0)
    .ToSql();

将被转译为:

SELECT a.[Id], a.[Name]
FROM [UserInfo] a
WHERE ((case when a.[Name] = N'LeaFrock' then 0 when a.[Name] > N'LeaFrock' then 1 else -1 end) > 0)

疑问:

目前转译表达式树的方式是自上而下单向的,似乎没办法结合表达式的父结点信息,来对整体生成的SQL做一些深度优化。比如使用string ExpressionLambdaToSqlCallString(MethodCallExpression exp, ExpTSC tsc)解析string.Compare(a.Name, "LeaFrock")时,无法充分利用上层的BinaryExpression(xxx > 0)的常量信息来简化SQL。在EF Core中则有类似的优化方式

@luoyunchong luoyunchong merged commit af22d76 into dotnetcore:master Apr 10, 2026
1 check passed
2881099 added a commit that referenced this pull request Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants