How do I do this
Select top 10 Foo from MyTable
in Linq to SQL?
This works well in C#
var q = from m in MyTable.Take(10)
select m.Foo
Taking data of DataBase without sorting is the same as random take
You would use the Take(N) method.
Use the Take(int n)
method:
var q = query.Take(10);
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();
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.