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

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

How do I do this

Select top 10 Foo from MyTable

in Linq to SQL?

相关标签:
14条回答
  • 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<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.

    0 讨论(0)
  • 2020-12-12 15:44

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

    0 讨论(0)
  • 2020-12-12 15:46

    Whether the take happens on the client or in the db depends on where you apply the take operator. If you apply it before you enumerate the query (i.e. before you use it in a foreach or convert it to a collection) the take will result in the "top n" SQL operator being sent to the db. You can see this if you run SQL profiler. If you apply the take after enumerating the query it will happen on the client, as LINQ will have had to retrieve the data from the database for you to enumerate through it

    0 讨论(0)
  • 2020-12-12 15:48

    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
                    };
    
    0 讨论(0)
  • 2020-12-12 15:49

    This way it worked for me:

    var noticias = from n in db.Noticias.Take(6)
                           where n.Atv == 1
                           orderby n.DatHorLan descending
                           select n;
    
    0 讨论(0)
  • 2020-12-12 15:50

    In VB:

    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.

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