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.Columns.Add("Column1");
            dt.Columns.Add("Column2");
            dt.Columns.Add("Column3");
            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,

Column1Column2Column3
ABC
DEF

To get the last two rows use the following query,

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

Output:

Column1Column2Column3
JKL
GHI


Subscribe to GET LATEST ARTICLES!


Related

Dot Net 2176976618474508337

Post a Comment

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

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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

    ReplyDelete
  4. І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.

    ReplyDelete
  5. Fantastic article. It has great value. Accounting And Bookkeeping Services In Dubai Students in Singapore who take English lessons are preparing for their future occupations as well as learning how to manage their money.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Hey there! If you're eager to dive into the world of painting, visiting the AmberValley is your ticket to unrivaled artistic exploration. With its high-potential bonuses for new players, you may even strike it lucky and be rewarded with not only amazing artwork but also substantial cash prizes.

    ReplyDelete
  8. This is a very useful article for those who love code wii roms

    ReplyDelete
  9. This article proves to be incredibly beneficial for coding enthusiasts. kleurplaat lol



    ReplyDelete

emo-but-icon

SUBSCRIBE


item