Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I limit the number of rows displayed

I construct the below query to display data from Json.
Assume this query returns 50 rows. But I want only first 10 rows to be displayed.
What is the equivalent of select top 10 or limit 10 in this scenario. Thank you.

List<Item> result = items.GroupBy(x => x.ItemNo)
       .Select(x => new Item
        {
            ItemNo = x.Key,
            ItemName = x.FirstOrDefault(y => y.Key == "ItemName")?.Value,           
            Date = x.FirstOrDefault(y => y.Key == "Date")?.Value
        }).OrderByDescending(y => y.date)
.ToList();

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I think that the method you are looking for is Take

Take method from Microsoft website

In the end I think your code should look like this:

List<Item> result = items.GroupBy(x => x.ItemNo)
   .Select(x => new Item
    {
        ItemNo = x.Key,
        ItemName = x.FirstOrDefault(y => y.Key == "ItemName")?.Value,           
        Date = x.FirstOrDefault(y => y.Key == "Date")?.Value
    }).OrderByDescending(y => y.date).Take(10).ToList();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading