Skip to content

Conversation

@rstam
Copy link
Contributor

@rstam rstam commented Oct 7, 2025

No description provided.

@rstam rstam requested a review from a team as a code owner October 7, 2025 19:42
var strATranslation = ExpressionToAggregationExpressionTranslator.Translate(context, strAExpression);
var strBExpression = arguments[1];
var strBTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, strBExpression);
var ast = AstExpression.Cmp(strATranslation.Ast, strBTranslation.Ast);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add support for case insensitive/more overloads comparison by translating to $strcasecmp?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var strATranslation = ExpressionToAggregationExpressionTranslator.Translate(context, strAExpression);
var strBExpression = arguments[1];
var strBTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, strBExpression);
var ast = AstExpression.Cmp(strATranslation.Ast, strBTranslation.Ast);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@adelinowona adelinowona Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this class was extended to support the static string.Compare method, it seems its name is a bit outdated now? I would suggest updating the class name though I don't have a good suggestion maybe StringComparisonExpressionToFilterTranslator?

throw new ExpressionNotSupportedException(expression);
}

private static bool IsStaticCompareMethod(MethodInfo method)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IsStaticCompareMethod doesn't actually check if the method is named "Compare". I doubt this is intentional?

Copy link
Contributor Author

@rstam rstam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did some refactoring and added support for the overload of Compare that has an ignoreCase parameter.


internal static class AstComparisonFilterOperatorExtensions
{
public static AstComparisonFilterOperator GetComparisonOperatorForSwappedLeftAndRight(this AstComparisonFilterOperator @operator)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put here so that it can be used from more than one place.

return false;
}

public static bool IsInstanceCompareToMethod(this MethodInfo method)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put here so that it can be used from more than one place.


namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators
{
internal static class CompareMethodToAggregationExpressionTranslator
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare and CompareTo handling is consolidated into a single class.


namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionTranslators
{
internal static class CompareComparisonExpressionToFilterTranslator
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare and CompareTo handling is consolidated into a single class.

{
Expression value1Expression;
Expression value2Expression;
if (method.IsStatic)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main difference between Compare and CompareTo is whether it's a static method or not.


Expression fieldExpression;
Expression innerValueExpression;
if (compareMethod.IsStatic)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main difference between Compare and CompareTo is whether it's a static method or not.

}
}

private static AstComparisonFilterOperator GetComparisonOperatorForSwappedLeftAndRight(Expression expression)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to an extension method so it can be used from more than one class.

[InlineData(11, false, "{ $match : { B : { $gte : 'A' } } }", new int[] { 1, 2, 3, 4, 5, 6 })]
[InlineData(12, false, "{ $match : { B : { $lte : 'A' } } }", new int[] { 1, 3 })]
[InlineData(1, true, "{ $match : { $expr : { $eq : [{ $strcasecmp : ['A', '$B'] }, -1] } } }", new int[] { 2, 5 })]
[InlineData(2, true, "{ $match : { $expr : { $eq : [{ $strcasecmp : ['A', '$B'] }, 0] } } }", new int[] { 1, 3, 4, 6 })]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one might have been possible without using $expr by using regular expressions but I don't think it's worth the effort.

sanych-sun
sanych-sun previously approved these changes Oct 22, 2025
Copy link
Member

@sanych-sun sanych-sun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM + minor comments


namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira;

public class CSharp5730Tests : LinqIntegrationTest<CSharp5730Tests.ClassFixture>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have tests for CompareTo and IComparable<T>.CompareTo?


var value1Translation = ExpressionToAggregationExpressionTranslator.Translate(context, value1Expression);
var value2Translation = ExpressionToAggregationExpressionTranslator.Translate(context, value2Expression);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check if the value1 and value2 has string representation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants