Skip to content
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

Using Max requires a cast to object #169

Closed
bobbymthompson opened this issue May 31, 2018 · 4 comments
Closed

Using Max requires a cast to object #169

bobbymthompson opened this issue May 31, 2018 · 4 comments
Assignees
Labels

Comments

@bobbymthompson
Copy link

bobbymthompson commented May 31, 2018

Using this test code:

NetStandardDbContext context = new NetStandardDbContext();

var typed = context.Issues
	.Where(i => context.Issues.Max(x => x.RevisionDate) == i.RevisionDate)
	.Select(i => new { i.Title, i.RevisionDate })
	.ToList();

var success = context.Issues
	.Where("@0.Max(RevisionDate) == object(RevisionDate)", context.Issues)
	.Select("new(Title, RevisionDate)")
	.ToDynamicList();

var failure = context.Issues
	.Where("@0.Max(RevisionDate) == RevisionDate", context.Issues)
	.Select("new(Title, RevisionDate)")
	.ToDynamicList();

Which generates:

select title, revisiondate from issues i where (select max(revisiondate) from issues) = i.revisiondate

Using Max as a dynamic where clause, the following exception occurs:

System.Linq.Dynamic.Core.Exceptions.ParseException: 'Operator '==' incompatible with operand types 'Object' and 'DateTime?''

If you cast the right expression to object, the comparison succeeds. I do not believe that the left expression should be cast to object.

I traced this to ExpressionParser::ParseAggregate

Type callType = typeof(Enumerable);
if (isQueryable && MethodFinder.ContainsMethod(typeof(IQueryableSignatures), methodName, false, args))
{
     callType = typeof(Queryable);
}

Upon calling into this method it will change the args Type from a Nullable DateTime to an object. Stepping over this bypasses the issue.

@StefH
Copy link
Collaborator

StefH commented Jun 1, 2018

Quick question: do you get the same error when using DateTime instead a nullable DateTime ?

@bobbymthompson
Copy link
Author

Yes. It doesn't matter if it is nullable or not.

@StefH
Copy link
Collaborator

StefH commented Nov 20, 2019

@striker8118 is this still an issue for you?

@StefH StefH self-assigned this Sep 10, 2023
@StefH
Copy link
Collaborator

StefH commented Sep 10, 2023

Hello @bobbymthompson,
I'm not able to reproduce this using the latest version. I'll close this issue.

[Fact]
public void Max_Where_On_DateTime()
{
    // Arrange
    var users = User.GenerateSampleModels(10);

    // Act
    var typed = users
        .Where(u => users.Max(m => m.BirthDate) == u.BirthDate)
        .ToList();
    var dynamic = users.AsQueryable()
        .Where("@0.Max(BirthDate) == BirthDate", users)
        .ToList();

    // Assert
    dynamic.Should().BeEquivalentTo(typed);
}

[Fact]
public void Max_Where_On_NullableDateTime()
{
    // Arrange
    var users = User.GenerateSampleModels(10);

    // Act
    var typed = users
        .Where(u => users.Max(m => m.EndDate) == u.EndDate)
        .ToList();
    var dynamic = users.AsQueryable()
        .Where("@0.Max(EndDate) == EndDate", users)
        .ToList();

    // Assert
    dynamic.Should().BeEquivalentTo(typed);
}

#744

@StefH StefH closed this as completed Sep 10, 2023
@StefH StefH added the question label Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants