linq select first entries on x in csharp

You can use the LINQ First() method to select the first element or the first element that satisfies a given condition within a collection.

To select the first X number of entries in a collection, you can use the Take() method in combination with First(). Here's how you can do it:

main.cs
// create an example list of integers
var numbers = Enumerable.Range(1, 10);

// select the first 5 entries using Take() and First()
var first5 = numbers.Take(5).First();

// output the results to the console
Console.WriteLine(first5);
236 chars
9 lines

This will output 1 in the console, which is the first element of the numbers collection after taking 5 elements.

gistlibby LogSnag