Forums - C# Corner

Forum guidelines
Mani Kandan

Mani Kandan

  • 514
  • 2k
  • 96.1k

how to take first 10 record using LINQ ?

Apr 21 2017 6:02 AM
Hello,
 
If there is have more than 10 records, then take only 10,
  1. var query = from a in context.Advertisements where a.idPartner == partnerid select a;  
  2.                   count = query.Count();  
  3.                   if (count > 10)  
  4.                        return (query.Skip(startIndex).Take(10).ToList());                       
  5.                   else  
  6.                       return query.ToList(); 
 
now the error occured,
 
The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.
 
Then I have tried, this code:
  1. return (query.OrderBy(p=>p.idAdvertisement).Skip(startIndex).Take(10).ToList()); 
 But here occurred another error,
 
Incorrect syntax near 'OFFSET'.
Invalid usage of the option NEXT in the FETCH statement. 
 
How can I solve this ?
 

Answers (4)