Abhilash J A

Abhilash J A

  • 533
  • 2.4k
  • 598.2k

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)