Get top N records using LINQ to Entities

前端 未结 2 1647
梦毁少年i
梦毁少年i 2020-12-09 15:26

I am using Linq to entities and would like to know if I can get a limited number of records when i query. I just need the top N records as the query do the orderby and other

相关标签:
2条回答
  • 2020-12-09 16:13

    There are multiple ways

    1)

    var data = (from p in db.people  
                orderby p.IdentityKey descending 
                select p).Take(100); 
    

    2)

    var query = db.Models.Take(100);
    

    3) or you can skip certain results

    var data = (from p in people
                select p).Skip(100);
    
    0 讨论(0)
  • 2020-12-09 16:31

    You can just use the .Take method call to get a couple of result. You can read more on this topic here.

    You need to understand that the query will not be executed unless someone executes the GetEnumerator().

    0 讨论(0)
提交回复
热议问题