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

前端 未结 14 1232
野性不改
野性不改 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:51

    This works well in C#

    var q = from m in MyTable.Take(10)
            select m.Foo
    
    0 讨论(0)
  • 2020-12-12 15:51

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

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

    You would use the Take(N) method.

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

    Use the Take(int n) method:

    var q = query.Take(10);
    
    0 讨论(0)
  • 2020-12-12 15:57
    Array oList = ((from m in dc.Reviews
                               join n in dc.Users on m.authorID equals n.userID
                               orderby m.createdDate descending
                               where m.foodID == _id                      
                               select new
                               {
                                   authorID = m.authorID,
                                   createdDate = m.createdDate,
                                   review = m.review1,
                                   author = n.username,
                                   profileImgUrl = n.profileImgUrl
                               }).Take(2)).ToArray();
    
    0 讨论(0)
  • 2020-12-12 15:59

    The OP actually mentioned offset as well, so for ex. if you'd like to get the items from 30 to 60, you would do:

    var foo = (From t In MyTable
           Select t.Foo).Skip(30).Take(30);
    

    Use the "Skip" method for offset.
    Use the "Take" method for limit.

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