.Skip().Take() on Entity Framework Navigation Properties is executing SELECT * on my SQL Server

后端 未结 2 1625
别跟我提以往
别跟我提以往 2021-02-02 03:32

I have a method on my generated partial class like this:

var pChildren = this.Children
    .Skip(skipRelated)
    .Take(takeRelated)
    .ToList();
2条回答
  •  不知归路
    2021-02-02 03:34

    The problem is that you are performing a LINQ-to-Object query when you query a child collection like that. EF will load the whole collection and perform the query in memory.

    If you are using EF 4 you can query like this

    var pChildren = this.Children.CreateSourceQuery()
                     .OrderBy(/* */).Skip(skipRelated).Take(takeRelated);
    

    In EF 4.1

    var pChildren = context.Entry(this)
                       .Collection(e => e.Children)
                       .Query()
                       .OrderBy(/* */).Skip(skipRelated).Take(takeRelated)
                       .Load();
    

提交回复
热议问题