efcore icon indicating copy to clipboard operation
efcore copied to clipboard

InMemory: When adding subquery projection, take into account client projection

Open smitpatel opened this issue 7 years ago • 10 comments

See https://github.com/aspnet/EntityFrameworkCore/issues/16963#issuecomment-528037843

smitpatel avatar Sep 04 '19 20:09 smitpatel

So how exactly are we supposed to work around this? Using an anonymous object doesn't work either, and we can't create tuples in expressions?

We are just now attempting to update to dotnet 3 (from 2.2), and we have a bunch of failing tests because of this..

zlepper avatar Jan 23 '20 11:01 zlepper

any update on this?

tomasbruckner avatar Feb 20 '20 09:02 tomasbruckner

I'm getting this exact error, but in a different, quite specific, situation.

Scenario: Fetching a Parent-ChildCollection and projecting to new Parent-ChildAggregate with a nested Parent.OwnedEntityProperty.

Json representation of source Parent entity structure: { "Config": { "SubConfig": { } } "Child": [] }

Whenever I try aggregate the child list AND retrieve the nested value object, I get the Unable to cast object of type 'System.Linq.Expressions.NewExpression' to type 'System.Linq.Expressions.MethodCallExpression' error.

I created a simple console app and proved that this is the only scenario it happens in.

  • No child aggregate, just nested config -> passes
  • Just child aggregate, no config -> passes
  • Child Aggregate, with non-nested config -> passes
  • Child list, with nested config -> passes
  • Child aggregate, with nested config -> FAILS

In my real project the last scenario succeeds against SQL Server, I have not tried this sample against SQL.

EFCoreTests.zip

To work around this I am checking if the Db is in memory, then split the query into 2. That way my tests pass, and I still have the performance of a single read to the real Db.

brenwebber avatar Mar 09 '20 18:03 brenwebber

Really keen to hear if there is an ETA for this. It's a blocker for our migration to 3.1 at this point.

PoyaManouchehri avatar Mar 27 '20 00:03 PoyaManouchehri

For anyone coming across this because they're trying to use a .Count alongside a parameterized constructor (like my issue https://github.com/dotnet/efcore/issues/21001 ), we've worked around this by wrapping the Count in a Convert.ToInt32 call. This fixes the tests that use the InMemory provider. In terms of the SQL generated for the SQL Server provider, this adds a CONVERT. Someone who is more of an expert on SQL perf can correct me if I'm wrong, but I don't think that should adversely affect performance, and for us it's superior to dropping to parameterless ctors.

@PoyaManouchehri don't know if this helps you?

jgbpercy avatar Jul 21 '20 10:07 jgbpercy

I have this exception used Select and Count together.

var test = await db.Users.Select(u => new
                {
                    u.Id,
                    Acts = u.Activities.Select(a => new { a.Id }),
                    ActsCount = u.Activities.Count(),
                }).ToListAsync();

kvv842 avatar Aug 03 '20 13:08 kvv842

@jgbpercy I can confirm that our original issue (exception when using .Any() in a subquery) is fixed in EF Core InMemory 3.1.6.

PoyaManouchehri avatar Aug 04 '20 04:08 PoyaManouchehri

.Any() is still a problem for me in EF Core InMemory 3.1.7. When I replace .any() with .Count() > 0 my tests pass.

Noppey avatar Aug 17 '20 12:08 Noppey

This still fails in 3.1.8 in InMemoryDatabase but works fine in SQL Server/PostgreSQL.

Exception

Unable to cast object of type 'System.Linq.Expressions.NewExpression' to type 'System.Linq.Expressions.MethodCallExpression'.

code

public static IQueryable<ContainerDto> ToDtoList(this IQueryable<Container> data)
{
    return data.Select(AsDto);
}

public static readonly Expression<Func<Container, ContainerDto>> AsDto =
  item => new ContainerDto
{
    Text = item.Text,
    Cars = item.Cars
        .AsQueryable()
        .Select(AsCarDto)
        .FirstOrDefault(),
};

public static readonly Expression<Func<Car, CarDto>> AsCarDto =
  item => new CarDto
  {
      Name = item.Name,
  };

tomasbruckner avatar Sep 24 '20 15:09 tomasbruckner

I am having the same problem as reply above. Works in SQL Server but not InMemoryDatabase. Is there a known workaround?

Thanks!

ryannasr avatar Oct 11 '20 00:10 ryannasr