Skip to content

Translate as in where clause #17475

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Anduin2017 opened this issue Aug 28, 2019 · 7 comments
Closed

Translate as in where clause #17475

Anduin2017 opened this issue Aug 28, 2019 · 7 comments

Comments

@Anduin2017
Copy link
Member

I have two class:

public abstract class Conversation
{
    public int Id { get; set; }
}

public class PrivateConversation : Conversation
{
    public ICollection<User> Users { get; set; }
}

In DbContext, there are DbSets:

public DbSet<Conversation> Conversations { get; set; }
public DbSet<PrivateConversation> PrivateConversations { get; set; }
public DbSet<User> Users { get; set; }

Which generates two tables: Conversations and Users.

PrivateConversations are saved in table Conversations

Now an error occurred with query:


// For some reason, I can only query `db.Conversations`, can not access `db.PrivateConversations`
var conversations = db.Conversations
  .Include(t => (t as PrivateConversation).Users)
  .Where(t => t is PrivateConversation)
  .Where(t => (t as PrivateConversation).Users.Any())
  .ToList();

Told me that in Users.Any(), Users is null.

I am very confused. How can I query all private conversations with user in it?

Additional info:

packages I usesd:

<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SQLServer" Version="2.2.6" />

runtime: .NET Core 2.2

<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
@ajcvickers ajcvickers changed the title Conditionally query related entities with type converting Translate as in where clause Aug 30, 2019
@ajcvickers
Copy link
Contributor

@Anduin2017 EF translates .Where(t => ((PrivateConversation)t)... but not .Where(t => (t as PrivateConversation)....

@ajcvickers ajcvickers added this to the Backlog milestone Aug 30, 2019
@Anduin2017
Copy link
Member Author

Thanks! That really solves!

@Anduin2017
Copy link
Member Author

Ok. I gotta be really happy if you can fix this and translate the as grammar.

@smitpatel smitpatel added the good first issue This issue should be relatively straightforward to fix. label Sep 3, 2019
@SARAVANA1501
Copy link
Contributor

Can you tell me where I can add this new grammar?

@ajcvickers
Copy link
Contributor

@smitpatel Since you added good-first-issue here can you point @SARAVANA1501 in the right direction to submit a PR for this?

@smitpatel
Copy link
Contributor

While code is similar in various place, we can start with one location and update at all location once the structure is define.

https://github.com/aspnet/EntityFrameworkCore/blob/b7df776b0e1b2d94da48b0471c657fda8dbe8f9b/src/EFCore/Query/Internal/NavigationExpandingExpressionVisitor.ExpressionVisitors.cs#L90-L102

Above code basically unwrap a convert node and gives the converted type. Similar to above processing it needs to identify TypeAsOperation and then unwrap it to identify converted type and innerExpression. It should be added as extension method so all places can easily utilize it. So it goes this way.

Expression Inner ConvertedType
a.Property a null
((T)a).Property a T
(a as T).Property a T

@smitpatel
Copy link
Contributor

This is already fixed in master branch in #19377

@smitpatel smitpatel removed this from the Backlog milestone May 22, 2020
@ajcvickers ajcvickers added this to the 5.0.0-preview2 milestone May 22, 2020
@ajcvickers ajcvickers added closed-fixed and removed good first issue This issue should be relatively straightforward to fix. labels May 22, 2020
@smitpatel smitpatel self-assigned this May 22, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-preview2, 5.0.0 Nov 7, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

4 participants