tags:

views:

16719

answers:

9

How do I do this

Select top 10 Foo from MyTable

in Linq to SQL?

+21  A: 
from m in MyTable
take 10
select m.Foo

This assumes that MyTable implements IQueryable. You may have to access that through a DataContext or some other provider.

It also assumes that Foo is a column in MyTable that gets mapped to a property name.

See http://blogs.msdn.com/vbteam/archive/2008/01/08/converting-sql-to-linq-part-7-union-top-subqueries-bill-horst.aspx for more detail.

David Alpert
That doesn't work in C#, there is no take expression. You need to use the Take() method.
Adam Lassek
Technically, the questioner asked for Linq to SQL, so VB is a viable assumption. That said, ALassek, i'm a c# guy myself and prefer your answer. :-)
David Alpert
Well, you're example was written in C# LINQ which is why I pointed that out.
Adam Lassek
thanks, ALassek. I didn't sleep last night so i appreciate the catch in my grammar.
David Alpert
A: 

You would use the Take(N) method.

FlySwat
+46  A: 

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
Adam Lassek
The little differences in LINQ between C# and VB are annoying. Why doesn't C# have a take expression like VB? That seems like an oversight. And VB's lack of anonymous Subs makes lambdas much less useful.
Adam Lassek
Just what I was looking for +1
jasonco
+8  A: 

Use the Take(int n) method.

var q = query.Take(10);
amcoder
A: 

This works well in C#

var q = from m in MyTable.Take(10)
        select m.Foo
spdrcr911
+2  A: 

I do like this:

 var dados =  from d in dc.tbl_News.Take(4) 
                orderby d.idNews descending

                select new 
                {
                    d.idNews,
                    d.titleNews,
                    d.textNews,
                    d.dateNews,
                    d.imgNewsThumb
                };
Janei Vieira
The problem with this approach is that you will take 4 and then order them, when I suspect what you really want is to get the top 4 results. You need to do the take after the orderby, see Yanns comment.
Russell Troywest
A: 

Take is not exactly the same as TOP 10 as it forces a sort. Is there anyway to take an aribitrary top 10 without the sort?

Mike
A: 

Taking data of DataBase without sorting is the same as random take

Anton
+2  A: 

@Janei: my first comment here is about your sample ;)

I think if you do like this, you want to take 4, then applying the sort on these 4.

var dados =  from d in dc.tbl_News.Take(4) 
                orderby d.idNews descending
                select new 
                {
                    d.idNews,
                    d.titleNews,
                    d.textNews,
                    d.dateNews,
                    d.imgNewsThumb
                };

Different than sorting whole tbl_News by idNews descending and then taking 4

var dados =  (from d in dc.tbl_News
                orderby d.idNews descending
                select new 
                {
                    d.idNews,
                    d.titleNews,
                    d.textNews,
                    d.dateNews,
                    d.imgNewsThumb
                }).Take(4);

no ? results may be different.

Yann