Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

LeaFrock
Copy link

@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中则有类似的优化方式

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.

2 participants