[英]Entity Framework Core Include with Filter
I am trying to bring a list of objects that have a list of child objects as well from the database. 我试图从数据库中带来一个具有子对象列表的对象列表。
Here is an example 这是一个例子
public class User
{
public int Id { get; set; }
public ICollection<Child> Childs { get; set; }
}
public class Child
{
public int Id { get; set; }
public string UserId { get; set; }
public User User { get; set; }
}
The problem here is that I cannot find a way to bring a list of Users and filter the Childs with a condition as well at the same time. 这里的问题是我无法找到一种方法来提供用户列表并同时过滤具有条件的Childs。
I tried something like this: 我试过这样的事情:
users = _context.Users.Where(e => e.Childs.Any(ec => ec.Id > 1))
But using this example if the condition is not met it will not bring the User back and I want all the users even if they do not have Childs or the condition is not met. 但是如果不满足条件,则使用此示例将不会使用户返回并且我希望所有用户即使他们没有Childs或者条件不满足也是如此。
Also found this project: https://github.com/zzzprojects/EntityFramework-Plus but looks like it does not support EF Core for what I want to do. 还找到了这个项目: https : //github.com/zzzprojects/EntityFramework-Plus但看起来它不支持我想做的EF Core。
Does anyone have any suggestions? 有没有人有什么建议?
Disclaimer : I'm the owner of the project Entity Framework Plus 免责声明 :我是项目Entity Framework Plus的所有者
Our Library doesn't support Query Filter yet for .NET Core due to the N+1 queries issue. 由于N + 1查询问题,我们的库不支持.NET Core的查询过滤器。
Under our hood for EF6, our library was only doing a simple projection. 在我们的EF6引擎盖下,我们的图书馆只进行了简单的投影。
Something similar to this using your information: 使用您的信息与此类似的东西:
var users = _Context.Users.Select(x => new {
Users = x,
Childs = x.Childs.Any(ec => ec.Id > 1)
})
.ToList()
.Select(x => x.Users)
.ToList();
However, for EF Core, the same projection makes a database roundtrip to get a child for every user (N+1 queries) 但是,对于EF Core,相同的投影会使数据库往返以获取每个用户的子项(N + 1个查询)
You can try the following projection and see if you get the same error. 您可以尝试以下投影,看看是否得到相同的错误。
I believe until the EF Core team fixes it, to my knowledge, there is not way to filter child entities. 我相信,直到EF核心团队修复它,据我所知,没有办法过滤子实体。
SELECT [x].[Id], [x].[ColumnInt]
FROM [Lefts] AS [x]
go
exec sp_executesql N'SELECT CASE
WHEN EXISTS (
SELECT 1
FROM [Rights] AS [ec1]
WHERE ([ec1].[Id] > 1) AND (@_outer_Id1 = [ec1].[LeftId]))
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END',N'@_outer_Id1 int',@_outer_Id1=1
go
exec sp_reset_connection
go
exec sp_executesql N'SELECT CASE
WHEN EXISTS (
SELECT 1
FROM [Rights] AS [ec1]
WHERE ([ec1].[Id] > 1) AND (@_outer_Id1 = [ec1].[LeftId]))
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END',N'@_outer_Id1 int',@_outer_Id1=2
go
exec sp_reset_connection
go
exec sp_executesql N'SELECT CASE
WHEN EXISTS (
SELECT 1
FROM [Rights] AS [ec1]
WHERE ([ec1].[Id] > 1) AND (@_outer_Id1 = [ec1].[LeftId]))
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END',N'@_outer_Id1 int',@_outer_Id1=3
go
exec sp_reset_connection
go
exec sp_executesql N'SELECT CASE
WHEN EXISTS (
SELECT 1
FROM [Rights] AS [ec1]
WHERE ([ec1].[Id] > 1) AND (@_outer_Id1 = [ec1].[LeftId]))
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END',N'@_outer_Id1 int',@_outer_Id1=4
go
exec sp_reset_connection
go
exec sp_executesql N'SELECT CASE
WHEN EXISTS (
SELECT 1
FROM [Rights] AS [ec1]
WHERE ([ec1].[Id] > 1) AND (@_outer_Id1 = [ec1].[LeftId]))
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END',N'@_outer_Id1 int',@_outer_Id1=5
go
ANSWER Sub-Question: 答案子问题:
With the new release of .NET core 2.0 does this problem got fixed ?
随着.NET Core 2.0的新版本,这个问题得到了修复吗?
Unfortunately, Entity Framework is still not able to handle cast correctly with the V2.x 遗憾的是,Entity Framework仍然无法使用V2.x正确处理转换
By example, this LINQ that use the Cast method doesn't work: 例如,使用Cast方法的LINQ不起作用:
var ids = ctx.MyTables
.Cast<IMyTable>()
.Cast<MyTable>()
.Where(x => x.SomeKey.Equals(keyId))
.Select(x => x.MyFieldIntegerIWant)
.ToList();
EDIT: Status Update 编辑:状态更新
Unfortunately, the EF Core team has still not fixed the N+1 queries issue with projection. 不幸的是,EF Core团队仍未解决投影中的N + 1查询问题。
Looking at the latest status for the v3.0, I do not believe they plan to fix this kind of issue soon either: https://github.com/aspnet/EntityFrameworkCore/issues/10001#issuecomment-456581915 看看v3.0的最新状态,我不相信他们计划很快解决这类问题: https : //github.com/aspnet/EntityFrameworkCore/issues/10001#issuecomment-456581915
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.