jQuery and ASP.NET

June 26, 2010

Skip and Select Elements in a String Array using LINQ




Here’s how to skip and select elements in a string array using LINQ. In the example shown below, we will skip the first two elements of an array and select the next three.

C#

static void Main(string[] args)
{
string[] arr = {"One", "Two", "Three",
"Four", "Five", "Six",
"Seven", "Eight"};
var result = from x in arr
.Skip<string>(2)
.Take<string>(3)
select x;

foreach (string str in result)
{
Console.WriteLine("{0}", str);
}

Console.ReadLine();
}

VB.NET

Sub Main(ByVal args() As String)
Dim arr() As String = {"One", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight"}
Dim result = From x In arr.Skip(Of String)(2).Take(Of String)(3)
Select x

For Each str As String In result
Console.WriteLine("{0}", str)
Next str

Console.ReadLine()
End Sub

OUTPUT

Three
Four
Five



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "Skip and Select Elements in a String Array using LINQ"
 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions