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

Herb Caudill picture Herb Caudill · Oct 10, 2008 · Viewed 173.9k times · Source

How do I do this

Select top 10 Foo from MyTable

in Linq to SQL?

Answer

Adam Lassek picture Adam Lassek · Oct 10, 2008

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<TSource> 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.