Dot Net Capsules

A Capsule on .NET, everyday, keeps you .NetHealthy!!!!

My Photo
Name:

Programmer. Visit my new blog at www.ashishsheth.com

Friday, March 20, 2009

Checking an item is there in the array or not using LINQ

If you want to check whether an item is there in an array then you can use Linq instead of looping through the array.

Here is the sample code:

string[] selectedItems = items.Split(',');
int count = selectedItems.Count(s => s == id.ToString());

This code gives us the number of times the 'id' is there in the 'selectedItems' array.

Update:
You can find more Linq one liners here(http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=691):


Enjoy!!

Labels: , ,

0 Comments:

Post a Comment

<< Home

You Visitor Number:
Free Web Site Counter
Free Counter
free counters