-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Query: Unable to cast object of type when using Include in projection #8005
Comments
May already be fixed. @maumar to investigate. |
This is indeed fixed in current bits. @dvdobrovolskiy you don't actually need to add Include statements in the queries from the repro. |
Sorry @maumar for being dumb here but can you please explain last thig
and
so
only in I explicitly include them like |
@dvdobrovolskiy That's correct - you need to add Include statements if you are projecting entities in the final result. However, in the initial example, the projection was: new {
PersonId = s.PersonId,
FullName = s.FullName,
DOB = s.DOB,
cnt = _context.CorporatePersons.Include(i => i.Person).Where(a => a.Person.PersonId == s.PersonId).Count(),
}) it doesn't return the entire entity, just individual properties, and hence Include is not needed. Basically, the way we apply include is we look at the final projection and try to find entities that have include statements defined for them. If we don't find those entities in the final projections, Include is ignored completely. |
got it. Much appreciate @maumar |
code
Generates error
System.InvalidCastException: 'Unable to cast object of type '<_GroupJoin>d__264[CETDB.Data.CorporatePerson,CETDB.Data.Person,System.Nullable1[System.Int32],Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier2[CETDB.Data.CorporatePerson,System.Collections.Generic.IEnumerable1[CETDB.Data.Person]]]' to type 'System.Int32'.'
also
_context.Persons.Include(i => i.Level)
.Select(s => new {
PersonId = s.PersonId,
FullName = s.FullName,
DOB = s.DOB,
isCorporate = _context.CorporatePersons.Include(i=>i.Person).Any(a=>a.Person.PersonId == s.PersonId),
});
generates
InvalidOperationException: Sequence contains no elements
Models
The text was updated successfully, but these errors were encountered: