How to select top 5 records from List using LINQ in C#?


This content was proof-read by Grammarly. You can Try Grammary for FREE

Below is a sample code snippet demonstrating on how to select top 5 records from a list using LINQ in C# using the Take extension method…

How to select top 5 records from List using LINQ in C#?

using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

namespace AbundantCode

{

internal class Program

{

//How to select top 5 records from List using LINQ in C# ?

private static void Main(string[] args)

{

List<string> Names = new List<string>();

Names.Add("Abundantcode");

Names.Add("Programming");

Names.Add("Websites");

Names.Add("Coding");

Names.Add("Technologies");

Names.Add("USA");

Names.Add("C#");

Names.Add(".NET");

var result = (from m in Names

select m).Take(5);

foreach(var item in result)

Console.WriteLine(item);

Console.ReadLine();

}

}

}
%d bloggers like this: