C#.NET: Get Top and Bottom n Rows from DataTable using LINQ

LINQ, introduced in late 2007 is a way of querying data in arrays, lists, enumerable classes, XML,  relational database s and anything...

LINQ, introduced in late 2007 is a way of querying data in arrays, lists, enumerable classes, XML, relational databases and anything built on these.This post explains how to get the top and bottom n number of  records from C# DataTable without looping through each row using LINQ queries. A LINQ query operation consists of three actions: obtain the data source or sources, create the query, and execute the query.

For this example let me create a datatable with three columns and four rows such as this,

            DataTable dt = new DataTable();
            dt.Rows.Add("A", "B", "C");
            dt.Rows.Add("D", "E", "F");
            dt.Rows.Add("G", "H", "I");
            dt.Rows.Add("J", "K", "L");

To get the top two rows use the following query,

DataTable dt1 = dt.AsEnumerable().Take(2).CopyToDataTable();

This will produce the following output when bound to a gridview,


To get the last two rows use the following query,

DataTable dt1 = dt.AsEnumerable().Reverse().Take(2).CopyToDataTable();





Dot Net 2176976618474508337

Post a Comment

  1. Nice post! Educational content developer for K12 can face challenges while developing content, as every bit of information needs to be aligned to the learning objectives and the corresponding standards of the curriculum framework.
    copy editing services

  2. If you're fond of laying board games monopoly slots will certainly attract you with cool featured you might pretty appealing and worth trying


  3. HP printers offer top-notch offices to their clients around the world. In the event that you have additionally purchased another HP remote printer and don't have a 123.hp.com/setup clue how to set it up on your working framework, then, at that point, this nitty-gritty aide will prove to be useful. In this aide, we bring to you the best strategies and rules on the best way to set up HP Officejet Pro 6978 driver through its authority 123.hp.com/arrangement page. There are two different ways to associate an HP Officejet Pro 6978 with your Windows working framework. Design your new printer by visiting the www.123.hp.com/arrangement page through SSID and secret phrase, WPS ways take out your new HP Officejet Pro 6978 printer and turn it 'On'Open your Windows working framework on the opposite side and snap-on 'Start'Go to the 'Control Panel' and open the organizer of 'Organization and Connectivity' Snap on the 'HP Officejet Pro 6978 printer' symbol and push on the 'Settings' to open it. Select the wizard of 'Arrangement' and hit the catch of 'alright'

  4. Wonderful article. It's very useful. As a English tuition service provider, Students in Singapore are not only studying for their future careers, but also learning how to manage their finances. I find a very good website for the english tuition singapore, You can visit this site.

  5. Great post. Thanks so much for sharing your knowledge! It's great to see that some people still put in the effort to manage their websites. I will definitely be back soon.
    poppy playtime

  6. Іs established with a mission of helping students overcome pressing deadlines and a shortage of time. So, even the ordering process is how to do an argumentative essay made as easy as possible. The customer only needs to fill out an order form online to find a writer.