Wednesday, 20 January 2016

Any Function In Linq In C#

1/20/2016 - By Pranav Singh 1

In this article I will show you how you can use Any Method of LINQ in c#.net. In this I have used a list to perform the Any operation.


Any receives an argument of a proposition. It determines if any element in a collection matches with in a collection then only it returns the result otherwise it will not return any output. We can also perform this operation by using loop. But when we use look code size will get increased and it will become complex.

Now here is the code example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<State> objState = new List<State>();
            objState = GetStateData().Where(s => s.CityList.Any(m => m.Id == 3)).ToList();
        }
        public static List<State> GetStateData()
        {
            List<State> objState = new List<State>();
            List<City> objCity1 = new List<City>();
            objCity1.Add(new City { Id = 1, CityName = "City 11" });
            objCity1.Add(new City { Id = 2, CityName = "City 12" });

            objState.Add(new State { Id = 1, StateName = "State 1", CityList = objCity1 });

            List<City> objCity2 = new List<City>();
            objCity2.Add(new City { Id = 1, CityName = "City 11" });
            objCity2.Add(new City { Id = 3, CityName = "City 22" });

            objState.Add(new State { Id = 2, StateName = "State 2", CityList = objCity2 });

            return objState;
        }
    }
    public class State
    {
        public int Id { get; set; }
        public string StateName { get; set; }
        public List<City> CityList { get; set; }
    }
    public class City
    {
        public int Id { get; set; }
        public string CityName { get; set; }
    }
}


In above code I have create a city state collection. So when we try to get the record on that case we need to apply where clause on a collection inside a collection. Which is not possible. So we use Any for this.

Now First I will show what you get by passing city id 1:



Here you are getting both the record because the city is 1 exists in both the states.

Now when we pass the city id 3 on that case we are getting only one state value. Have a look of the output.



Tags: , ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

1 comment:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top