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

Filtering using .FirstOrDefault after a .Select always returns first row from the database #3106

Closed
sensoryoverload opened this issue Apr 2, 2024 · 1 comment

Comments

@sensoryoverload
Copy link

sensoryoverload commented Apr 2, 2024

The usage of a .Select in a query seems to ignore the filtering that comes after it. In case of a .FirstOrDefault it will always return the first row in the table. In case of a .SingleOrDefault it will throw "Sequence contains more than one element" because it didn't filter.

var test = session
    .Query<User>()
    .Select(u => new UserProjection
    {
        Id = u.Id,
        FirstName = u.FirstName,
        LastName = u.LastName,
    })
    .FirstOrDefault(x => x.FirstName == "Han" && x.LastName == "Solo 3");

Might be related to this ticket: #3096 but this one does not involve a .Include. And also #3009 but with a FirstOrDefault in this case. We use this type of query with or without filtering, but reusing the projection.

@jeremydmiller
Copy link
Member

It's the same issue as #3009. Can you just do the Where() then Select() then FirstOrDefault() as the workaround? We don't yet do the expression lifting like Relinq did automatically. Tbh, I was surprised anyone did this just knowing how things work internally

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

No branches or pull requests

2 participants