.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Lambda Expressions Extension Method 2 - Any()

Posted By:Jean Paul       Posted Date: May 05, 2011    Points: 200    Category: LINQ    URL: http://www.dotnetspark.com  

The Enumerable.cs class in .Net framework provides n number of useful methods. Let us see each methods one by one.
 

Lambda Expressions Extension Method 2 - Any()

The Enumerable.cs class in .Net framework provides n number of useful methods.  Let us see each methods one by one. 

Any()

The method Any() can be used to test whether any elements in the collection satisfies a particular condition. 

Please remember that the previous All() method ensures all the elements satisfy the condition, but the Any() method ensures just any of the elements satisfies the condition.

Return Value: Boolean

Example

In the following example we are using a list of integers and the Any () method is used to validate whether any of the  elements are equal to 5.

        private IList<int> _list = new List<int>();

 

        public InitializeList()

        {

            // Set data

            _list.Add(1);

            _list.Add(2);

            _list.Add(3);

            _list.Add(4);

            _list.Add(5);

            _list.Add(6);

        }

 

        private void AnyButton_Click(object sender, EventArgs e)

        {

            /// Any method to ensure atleast one elements == 5

            var result = _list.Any(i => i == 5);

 

            MessageBox.Show("Elements == 5: " + result.ToString());

        }

 

Extension Method Snapshot

You can find the snapshot of the Any() method usage as provided by Visual Studio



 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend