What's the Linq to SQL equivalent to TOP or LIMIT/OFFSET?

前端 未结 14 1267
野性不改
野性不改 2020-12-12 15:19

How do I do this

Select top 10 Foo from MyTable

in Linq to SQL?

14条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-12 15:41

    Use the Take method:

    var foo = (from t in MyTable
               select t.Foo).Take(10);
    

    In VB LINQ has a take expression:

    Dim foo = From t in MyTable _
              Take 10 _
              Select t.Foo
    

    From the documentation:

    Take enumerates source and yields elements until count elements have been yielded or source contains no more elements. If count exceeds the number of elements in source, all elements of source are returned.

提交回复
热议问题