實體框架核心包含過濾器

c# entity-framework entity-framework-core

我試圖從數據庫中帶來一個包含子對象列表的對象列表。

這是一個例子

 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; }
 }

這裡的問題是我找不到一種方法來提供用戶列表並同時過濾具有條件的Childs。

我試過這樣的事情:

users = _context.Users.Where(e => e.Childs.Any(ec => ec.Id > 1))

但是如果不滿足條件則使用此示例它將不會使用戶返回並且我希望所有用戶即使他們沒有Childs或者條件不滿足。

還找到了這個項目: https//github.com/zzzprojects/EntityFramework-Plus但看起來它並不支持EF Core我想做的事情。

有沒有人有任何建議?

謝謝

專家解答

免責聲明 :我是項目Entity Framework Plus的所有者

由於N + 1查詢問題,我們的庫不支持.NET Core的查詢過濾器。

在我們的EF6引擎蓋下,我們的圖書館只進行了簡單的投影。

使用您的信息與此類似的東西:

var users = _Context.Users.Select(x => new {
                Users = x,
                Childs = x.Childs.Any(ec => ec.Id > 1)
            })
            .ToList()
            .Select(x => x.Users)
            .ToList();

但是,對於EF Core,相同的投影會使數據庫往返以獲取每個用戶的子項(N + 1個查詢)

您可以嘗試以下投影,看看是否得到相同的錯誤。

我相信在EF Core團隊修復它之前,據我所知,沒有辦法過濾子實體。

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

答案子問題:

隨著.NET Core 2.0的新版本,這個問題得到了修復嗎?

不幸的是,Entity Framework仍然無法使用V2.x正確處理強制轉換

例如,使用Cast方法的LINQ不起作用:

var ids = ctx.MyTables
    .Cast<IMyTable>()
    .Cast<MyTable>()
    .Where(x => x.SomeKey.Equals(keyId))
    .Select(x => x.MyFieldIntegerIWant)
    .ToList();


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow