Click here to Skip to main content
13,046,533 members (69,431 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 2 Feb 2011

How to capture the effect of LIMIT clause in MySql using LINQ

, 3 Feb 2011
Rate this:
Please Sign up or sign in to vote.
Limit the number of records that can be fetched from database based on its count like using the LIMIT clause in MySql
Have you ever wondered that how you could get the same effect of the LIMIT clause in MySql in Linq?

Limit clause helps us to limit the number of records that can be fetched from database based on its count.

For example given a 'customerMaster ' table:

SELECT * FROM 'customerMaster '  LIMIT 0, 10 

This will display the first 10 results from the database.

SELECT * FROM 'customerMaster '  LIMIT 5, 5 

This will show records 6, 7, 8, 9, and 10.

Thus, it is really easier for us to create paginations, in various applications using MySql.

We can get the same effect by using LINQ. For this, we need to know two keywords, Skip and Take.

Consider the given example:

Given that 'customerMaster' is a business object containing the elements of the table 'customerMaster'.

//define a business object instance
 CustomerMaster customerMaster = new CustomerMaster();
//get all the list of customers
 List<CustomerMaster> _customerMasterList = customerMaster.GetAll();
//The linq below gets the list of customers between start and offset.
//i.e. if start=10 and offset=5, then it fetches the records starting from //11 to 15.
int start=10,offset=5;
 var query = (from _customerMaster in _customerMasterList
                       select _customerMaster).Skip(start).Take(offset).ToList();

Skip clause actually bypasses a specified number of elements in a sequence and then returns the remaining elements and Take returns a specified number of contiguous elements from the start of a sequence.

This helped to sort out my pagination problem. Hope it helps others too.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer Insync Tech Fin Solution Pvt Ltd
India India
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 3 Feb 2011
Article Copyright 2011 by debolina.d
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid