Search code examples
c#asp.net-mvc-3sql-server-2008-express

Adding Select Top in a Statement from a controller of ASP.net MVC 3


a question on how to add a "SELECT TOP" query in asp.net mvc3

            var applicant = from s in applicantRepository.GetApplicant()
                       select s;

in my applicant there is i think 200,000 datas and i just want to select the top 50

is that possible? Thanks KUDOS! :)


Solution

  • To return the top 50 rows in any order

    var applicant = 
        (from s in applicantRepository.GetApplicant()
        select s).Take(50);
    

    if you want to apply ordering, say there's a LastName field

    var applicant = 
        (from s in applicantRepository.GetApplicant()
        orderby s.LastName
        select s).Take(50);