You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When writing dynamic linq queries with expressions, mapping to objects with inherited properties, an exception is thrown,
The code below gives
Unhandled exception. System.InvalidOperationException: The LINQ expression 'Where<MyTable>(
source: DbSet<MyTable>,
predicate: (m) => new MyTableDto{
Id = m.Id,
Deleted = m.Deleted,
Name = m.Name
}
.Deleted == False)' 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.
Steps to reproduce
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Linq.Expressions;
namespace EFCoreSelectLinqBug
{
class Program
{
static void Main(string[] args)
{
var factory = LoggerFactory.Create(configure =>
{
configure.SetMinimumLevel(LogLevel.Trace);
configure.AddConsole();
});
var options = new DbContextOptionsBuilder<MyContext>()
.UseLoggerFactory(factory)
.UseSqlite("Datasource=:memory:");
using (var db = new MyContext(options.Options))
{
ParameterExpression parameter = Expression.Parameter(typeof(MyTableDto), "x");
MemberExpression property = Expression.Property(parameter, "Deleted");
Expression<Func<MyTableDto, bool>> selector = Expression.Lambda<Func<MyTableDto, bool>>(property, parameter);
var target = Expression.Constant(Convert.ToBoolean("false"));
var method = Expression.Equal(selector.Body, target);
Expression<Func<MyTableDto, bool>> lambda = Expression.Lambda<Func<MyTableDto, bool>>(method, selector.Parameters);
var rows = db.MyTables
.Select(x => new MyTableDto
{
Id = x.Id,
Deleted = x.Deleted,
Name = x.Name,
})
.Where(lambda)
.ToList();
}
}
}
public class MyContext : DbContext
{
public MyContext(DbContextOptions<MyContext> options)
: base(options)
{
}
public DbSet<MyTable> MyTables { get; set; }
}
public class MyTable
{
public int Id { get; set; }
public bool Deleted { get; set; }
public string Name { get; set; }
}
public class MyTableIdentityDto
{
public int Id { get; set; }
public bool Deleted { get; set; }
}
public class MyTableDto : MyTableIdentityDto
{
public string Name { get; set; }
}
}
Further technical details
EF Core version: 3.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer and Sqlite
Target framework: .NET Core 3.0
Operating system: Windows 10
The text was updated successfully, but these errors were encountered:
Make sure you are using same MemberInfo as in your complex type to create member access. This is not bug in EF as correct expression tree (as generated by compiler) works. And your dynamically generated expression tree is inaccurate.
When writing dynamic linq queries with expressions, mapping to objects with inherited properties, an exception is thrown,
The code below gives
Steps to reproduce
Further technical details
EF Core version: 3.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer and Sqlite
Target framework: .NET Core 3.0
Operating system: Windows 10
The text was updated successfully, but these errors were encountered: