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

后端 未结 2 1620
别跟我提以往
别跟我提以往 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:32

    Does it help if you call Skip on the result of Take? i.e.

    table.Take(takeCount+skipCount).Skip(skipCount).ToList()
    

    Also, see

    • TOP/LIMIT Support for LINQ?
    0 讨论(0)
  • 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();
    
    0 讨论(0)
提交回复
热议问题