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

LINQ expression can't be translated in unit test only #20226

Closed
stevendarby opened this issue Mar 9, 2020 · 3 comments
Closed

LINQ expression can't be translated in unit test only #20226

stevendarby opened this issue Mar 9, 2020 · 3 comments

Comments

@stevendarby
Copy link
Contributor

I build up a couple of LINQ expressions that should be translatable into SQL. In 2.2 this worked in the app and unit tests. In 3.1, it continues to work in the app - and I can see the SQL is executed as expected, with no client evaluation - however, in a unit test I get "The LINQ expression ... could not be translated."

I use SqlServer in the app and Sqlite in the unit test. Same context and configurations for both.

Steps to reproduce

Code adapted for an example:

public class Entity
{
    public string Property1 { get; set; }
    public int Property2 { get; set; }
}

Expression<Func<Entity, string>> filterMember = x => x.Property1;
Expression<Func<Entity, object>> selectMember = x => x.Property2;
const string filterVal = "testValue";

var parameter = filterMember.Parameters.First();
var filterMemberInvocation = Expression.Invoke(filterMember, parameter);
var equal = Expression.Equal(filterMemberInvocation, Expression.Constant(filterVal));
var where = Expression.Lambda<Func<Entity, bool>>(equal, parameter);
var max = context.Set<Entity>().Where(where).Max(selectMember);

Further technical details

EF Core version: 3.1
Database provider: Sqlite in unit tests
Target framework: 3.1
Operating system: Windows 10 1903
IDE: Visual Studio 2019 16.4.5

@smitpatel
Copy link
Contributor

Can you post whole exception message?

@stevendarby
Copy link
Contributor Author

stevendarby commented Mar 9, 2020

The LINQ expression 'DbSet<Entity>
    .Where(s => s.Property1 == "testValue")
    .Max(s => s.Property2)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

In trying to separate out a small reproducible solution I think I've discovered the crux of the issue. The 'selectMember' I'm using in the Max function is an int? property with a decimal? conversion for the DB. I believe Sqlite doesn't like filtering on decimals in 3.0/1+. For the test, I have added a double conversion for that property in a test DbContext that derives from my regular app DbContext, and this seems to have fixed it. This wasn't very clear from the exception.

@ajcvickers
Copy link
Contributor

@Snappyfoo Support for decimals on SQLite is being improved for 5.0: See #19635.

Duplicate of #11925

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
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

3 participants