开发者

Get top N records using LINQ to Entities

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 开发者_StackOverflow社区top N records as the query do the orderby and other clauses. Is this possible or I will have to get the top N using foreach loop?


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜