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 :
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();